serveur_websocket
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteProchaine révisionLes deux révisions suivantes | ||
serveur_websocket [2019/06/25 09:57] – [Installation de LibWebSockets] Mushussu | serveur_websocket [2019/06/25 10:02] – Mushussu | ||
---|---|---|---|
Ligne 45: | Ligne 45: | ||
<code c> | <code c> | ||
- | // gcc -Wall ServeurMinitelWeb.c -o ServeurMinitelWeb -Llibwebsockets/ | + | // gcc -Wall testServeur.c -o testServeur |
- | + | ||
- | + | ||
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
- | + | ||
static int | static int | ||
lws_callback_http( | lws_callback_http( | ||
- | + | | |
- | | + | struct lws *wsi, |
- | | + | enum lws_callback_reasons reason, |
- | | + | void *user, |
- | | + | void *in, |
- | | + | size_t len |
- | | + | ) { |
switch (reason) { | switch (reason) { | ||
case LWS_CALLBACK_ESTABLISHED: | case LWS_CALLBACK_ESTABLISHED: | ||
Ligne 74: | Ligne 74: | ||
((char *) in)[i]; | ((char *) in)[i]; | ||
} | } | ||
- | + | | |
printf(" | printf(" | ||
(int) len, buf + LWS_SEND_BUFFER_PRE_PADDING); | (int) len, buf + LWS_SEND_BUFFER_PRE_PADDING); | ||
- | + | | |
lws_write(wsi, | lws_write(wsi, | ||
- | len, LWS_WRITE_TEXT); | + | |
free(buf); | free(buf); | ||
break; | break; | ||
Ligne 86: | Ligne 86: | ||
break; | break; | ||
} | } | ||
- | + | | |
return 0; | return 0; | ||
} | } | ||
- | + | ||
static struct lws_protocols protocols[] = { | static struct lws_protocols protocols[] = { | ||
{ | { | ||
Ligne 100: | Ligne 100: | ||
} | } | ||
}; | }; | ||
- | + | ||
static const struct lws_extension exts[] = { | static const struct lws_extension exts[] = { | ||
{ | { | ||
Ligne 109: | Ligne 109: | ||
{ NULL, NULL, NULL /* terminator */ } | { NULL, NULL, NULL /* terminator */ } | ||
}; | }; | ||
- | + | ||
int main(void) { | int main(void) { | ||
int port = 9000; | int port = 9000; | ||
Ligne 115: | Ligne 115: | ||
struct lws_context *context; | struct lws_context *context; | ||
int opts = 0; | int opts = 0; | ||
- | + | | |
// create libwebsocket context representing this server | // create libwebsocket context representing this server | ||
struct lws_context_creation_info info; | struct lws_context_creation_info info; | ||
- | + | | |
memset(& | memset(& | ||
info.port = port; | info.port = port; | ||
Ligne 134: | Ligne 134: | ||
info.uid = -1; | info.uid = -1; | ||
info.options = opts; | info.options = opts; | ||
- | + | | |
context = lws_create_context(& | context = lws_create_context(& | ||
- | + | | |
if (context == NULL) { | if (context == NULL) { | ||
fprintf(stderr, | fprintf(stderr, | ||
return -1; | return -1; | ||
} | } | ||
- | + | | |
printf(" | printf(" | ||
while (1) { | while (1) { | ||
lws_service(context, | lws_service(context, | ||
} | } | ||
- | + | | |
lws_context_destroy(context); | lws_context_destroy(context); | ||
- | + | | |
return 0; | return 0; | ||
} | } | ||
+ | |||
</ | </ | ||
+ | Compilation : | ||
+ | gcc -Wall testServeur.c -o testServeur -lwebsockets | ||
+ | Execution : | ||
+ | ./ | ||
+ | | ||
+ | Créer le fichier index.html avec le contenu suivant : | ||
<code html> | <code html> | ||
< | < | ||
Ligne 193: | Ligne 200: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | L' | ||
+ | |||
+ | WebSockets test doit être inscrit en vert ce qui signifie que la connection WebSocket fonctionne bien. | ||
+ | |||
+ | Amusez-vous ! | ||
{{tag> | {{tag> |
serveur_websocket.txt · Dernière modification : 2020/07/22 14:19 de Mushussu