Outils pour utilisateurs

Outils du site


serveur_websocket

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
Prochaine révisionLes deux révisions suivantes
serveur_websocket [2019/06/25 09:57] – [Installation de LibWebSockets] Mushussuserveur_websocket [2019/06/25 10:02] Mushussu
Ligne 45: Ligne 45:
  
 <code c> <code c>
-// gcc -Wall ServeurMinitelWeb.c -o ServeurMinitelWeb -Llibwebsockets/build/lib -lwebsockets +// gcc -Wall testServeur.c -o testServeur -lwebsockets 
-  + 
- +
 #include <stdio.h> #include <stdio.h>
 #include <stdlib.h> #include <stdlib.h>
 #include <libwebsockets.h> #include <libwebsockets.h>
- +
 static int static int
 lws_callback_http( lws_callback_http(
-  +                   
-                         struct lws *wsi, +                  struct lws *wsi, 
-                         enum lws_callback_reasons reason, +                  enum lws_callback_reasons reason, 
-                         void *user, +                  void *user, 
-                         void *in, +                  void *in, 
-                         size_t len +                  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("received data: %s, replying: %.*s\n", (char *) in,             printf("received data: %s, replying: %.*s\n", (char *) in,
                    (int) len, buf + LWS_SEND_BUFFER_PRE_PADDING);                    (int) len, buf + LWS_SEND_BUFFER_PRE_PADDING);
- +            
             lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],             lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
-                               len, LWS_WRITE_TEXT);+                      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(&info, 0, sizeof info);     memset(&info, 0, sizeof info);
     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(&info);     context = lws_create_context(&info);
- +    
     if (context == NULL) {     if (context == NULL) {
         fprintf(stderr, "libwebsocket init failed\n");         fprintf(stderr, "libwebsocket init failed\n");
         return -1;         return -1;
     }     }
- +    
     printf("starting server...\n");     printf("starting server...\n");
     while (1) {     while (1) {
         lws_service(context, 50);         lws_service(context, 50);
     }     }
- +    
     lws_context_destroy(context);     lws_context_destroy(context);
- +    
     return 0;     return 0;
 } }
 +
 </code> </code>
  
 +Compilation :
 +  gcc -Wall testServeur.c -o testServeur -lwebsockets
 +Execution :
 +  ./testServeur
 +  
 +Créer le fichier index.html avec le contenu suivant :
 <code html> <code html>
 <!DOCTYPE html> <!DOCTYPE html>
Ligne 193: Ligne 200:
 </html> </html>
 </code> </code>
 +
 +L'ouvrir dans votre navigateur préféré. 
 +
 +WebSockets test doit être inscrit en vert ce qui signifie que la connection WebSocket fonctionne bien.
 +
 +Amusez-vous !
 {{tag>raspberry_pi serveur sylvain}} {{tag>raspberry_pi serveur sylvain}}
serveur_websocket.txt · Dernière modification : 2020/07/22 14:19 de Mushussu