Commit 36467d59d6ecfec9018199e5230153ac4298eecb
1 parent
664f7f67
Fichier d'exemple d'utilisation des websockets côté serveur.
Showing
1 changed file
with
0 additions
and
102 deletions
Show diff stats
prog/websockets.c deleted
@@ -1,102 +0,0 @@ | @@ -1,102 +0,0 @@ | ||
1 | -#include <stdio.h> | ||
2 | -#include <stdlib.h> | ||
3 | -#include <string.h> | ||
4 | -#include <libwebsockets.h> | ||
5 | - | ||
6 | -#define MAX_FRAME_SIZE 1024 | ||
7 | -#define WAIT_DELAY 50 | ||
8 | - | ||
9 | - | ||
10 | -static int callback_http(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len) | ||
11 | -{ | ||
12 | - return 0; | ||
13 | -} | ||
14 | - | ||
15 | -static int callback_my(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len) | ||
16 | -{ | ||
17 | - static char *message = NULL; | ||
18 | - static char *reponse = NULL; | ||
19 | - static int msize=0; | ||
20 | - switch(reason) | ||
21 | - { | ||
22 | - case LWS_CALLBACK_ESTABLISHED: | ||
23 | - printf("connection established\n"); | ||
24 | - message=NULL; | ||
25 | - // Declenchement d'un prochain envoi au navigateur | ||
26 | - libwebsocket_callback_on_writable(this,wsi); | ||
27 | - break; | ||
28 | - | ||
29 | - case LWS_CALLBACK_RECEIVE: | ||
30 | - // Ici sont traites les messages envoyes par le navigateur | ||
31 | - printf("received data: %s\n",(char *)in); | ||
32 | - message=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING); | ||
33 | - | ||
34 | - if(message==NULL) | ||
35 | - { | ||
36 | - perror("callback_my.malloc"); exit(EXIT_FAILURE); | ||
37 | - } | ||
38 | - | ||
39 | - memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,in,len); | ||
40 | - // Declenchement d'un prochain envoi au navigateur | ||
41 | - msize=len; | ||
42 | - libwebsocket_callback_on_writable(this,wsi); | ||
43 | - break; | ||
44 | - | ||
45 | - case LWS_CALLBACK_SERVER_WRITEABLE: | ||
46 | - // Ici sont envoyes les messages au navigateur | ||
47 | - if(message!= NULL) | ||
48 | - { | ||
49 | - char *text = "number : "; | ||
50 | - int length = strlen (text); | ||
51 | - unsigned char *buf = malloc(LWS_SEND_BUFFER_PRE_PADDING + length + LWS_SEND_BUFFER_POST_PADDING); | ||
52 | - memcpy (buf + LWS_SEND_BUFFER_PRE_PADDING, text, length ); | ||
53 | - libwebsocket_write(wsi, buf + LWS_SEND_BUFFER_PRE_PADDING, length, LWS_WRITE_TEXT); | ||
54 | - free(buf); | ||
55 | - | ||
56 | - free(message); | ||
57 | - message = NULL; | ||
58 | - } | ||
59 | - | ||
60 | - break; | ||
61 | - default: | ||
62 | - break; | ||
63 | - } | ||
64 | - return 0; | ||
65 | -} | ||
66 | - | ||
67 | -static struct libwebsocket_protocols protocols[] = { | ||
68 | - { | ||
69 | - "http-only", // name | ||
70 | - callback_http, // callback | ||
71 | - 0, // data size | ||
72 | - 0 // maximum frame size | ||
73 | - }, | ||
74 | - {"myprotocol",callback_my,0,MAX_FRAME_SIZE}, | ||
75 | - {NULL,NULL,0,0} | ||
76 | -}; | ||
77 | - | ||
78 | -int main(void) | ||
79 | -{ | ||
80 | - int port=9000; | ||
81 | - struct lws_context_creation_info info; | ||
82 | - memset(&info,0,sizeof info); | ||
83 | - info.port=port; | ||
84 | - info.protocols=protocols; | ||
85 | - info.gid=-1; | ||
86 | - info.uid=-1; | ||
87 | - struct libwebsocket_context *context=libwebsocket_create_context(&info); | ||
88 | - | ||
89 | - if(context==NULL) | ||
90 | - { | ||
91 | - fprintf(stderr, "libwebsocket init failed\n"); | ||
92 | - return -1; | ||
93 | - } | ||
94 | - printf("starting server...\n"); | ||
95 | - | ||
96 | - while(1) | ||
97 | - { | ||
98 | - libwebsocket_service(context,WAIT_DELAY); | ||
99 | - } | ||
100 | - libwebsocket_context_destroy(context); | ||
101 | - return 0; | ||
102 | -} |