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édente | ||
serveur_websocket [2019/06/20 13:10] – [Installation de LibWebSockets] Mushussu | serveur_websocket [2020/07/22 14:19] (Version actuelle) – Mushussu | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== | + | ====== |
Un tutoriel permettant de débuter avec la librairie LibWebSocket a été rédigé en anglais à cette [[https:// | Un tutoriel permettant de débuter avec la librairie LibWebSocket a été rédigé en anglais à cette [[https:// | ||
- | Il paraissait répondre aux attentes des débutants, mais certaine partie | + | Il paraissait répondre aux attentes des débutants, mais certaines parties |
Voici donc une réactualisation du tutoriel. | Voici donc une réactualisation du tutoriel. | ||
Matériel : | Matériel : | ||
- | * Liste à puceRaspberry | + | * Raspberry |
* Carte SD 16 Go | * Carte SD 16 Go | ||
Ligne 26: | Ligne 26: | ||
sudo apt-get install git-core | sudo apt-get install git-core | ||
sudo apt-get install libssl-dev | sudo apt-get install libssl-dev | ||
+ | sudo apt-get install zlib1g-dev | ||
| | ||
- | === LibWebWsockets | + | === LibWebsockets |
+ | |||
+ | Ce tutoriel fonctionne avec la version 3.2 de LibWebSocket, | ||
git clone https:// | git clone https:// | ||
cd libwebsockets | cd libwebsockets | ||
+ | git checkout v3.2-stable | ||
mkdir build | mkdir build | ||
cd build | cd build | ||
- | cmake .. -DLWS_IPV6=OFF | + | cmake .. -DLWS_IPV6=OFF -DLWS_MAX_SMP=8 -DLWS_WITHOUT_EXTENSIONS=OFF |
make | make | ||
sudo make install | sudo make install | ||
cd | cd | ||
+ | sudo ldconfig | ||
Pour verifier que tout fonctionne bien | Pour verifier que tout fonctionne bien | ||
libwebsockets-test-server | libwebsockets-test-server | ||
Taper dans votre navigateur préféré : | Taper dans votre navigateur préféré : | ||
- | http://serveurwebsocket:7681/ | + | http://127.0.0.1:7681/ |
Si le compteur démarre, alors la librairie est bien installée. | Si le compteur démarre, alors la librairie est bien installée. | ||
+ | Voici le code mis à jour : | ||
+ | |||
+ | <code c> | ||
+ | // gcc -Wall testServeur.c -o testServeur -lwebsockets | ||
+ | |||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | static int | ||
+ | lws_callback_http( | ||
+ | | ||
+ | struct lws *wsi, | ||
+ | enum lws_callback_reasons reason, | ||
+ | void *user, | ||
+ | void *in, | ||
+ | size_t len | ||
+ | ) { | ||
+ | switch (reason) { | ||
+ | case LWS_CALLBACK_ESTABLISHED: | ||
+ | printf(" | ||
+ | break; | ||
+ | case LWS_CALLBACK_RECEIVE: | ||
+ | unsigned char *buf = (unsigned char*) | ||
+ | malloc(LWS_SEND_BUFFER_PRE_PADDING + len | ||
+ | + LWS_SEND_BUFFER_POST_PADDING); | ||
+ | int i; | ||
+ | for (i=0; i < len; i++) { | ||
+ | buf[LWS_SEND_BUFFER_PRE_PADDING + (len - 1) - i ] = | ||
+ | ((char *) in)[i]; | ||
+ | } | ||
+ | | ||
+ | printf(" | ||
+ | (int) len, buf + LWS_SEND_BUFFER_PRE_PADDING); | ||
+ | | ||
+ | lws_write(wsi, | ||
+ | len, LWS_WRITE_TEXT); | ||
+ | free(buf); | ||
+ | break; | ||
+ | } | ||
+ | default: | ||
+ | break; | ||
+ | } | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | static struct lws_protocols protocols[] = { | ||
+ | { | ||
+ | " | ||
+ | lws_callback_http, | ||
+ | 0 // per_session_data_size | ||
+ | }, | ||
+ | { | ||
+ | NULL, NULL, 0 /* End of list */ | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | static const struct lws_extension exts[] = { | ||
+ | { | ||
+ | " | ||
+ | lws_extension_callback_pm_deflate, | ||
+ | " | ||
+ | }, | ||
+ | { NULL, NULL, NULL /* terminator */ } | ||
+ | }; | ||
+ | |||
+ | int main(void) { | ||
+ | int port = 9000; | ||
+ | const char *interface = NULL; | ||
+ | struct lws_context *context; | ||
+ | int opts = 0; | ||
+ | | ||
+ | // create libwebsocket context representing this server | ||
+ | struct lws_context_creation_info info; | ||
+ | | ||
+ | memset(& | ||
+ | info.port = port; | ||
+ | info.iface = interface; | ||
+ | info.protocols = protocols; | ||
+ | info.extensions = exts; | ||
+ | //if (!use_ssl) { | ||
+ | info.ssl_cert_filepath = NULL; | ||
+ | info.ssl_private_key_filepath = NULL; | ||
+ | //} else { | ||
+ | // info.ssl_cert_filepath = LOCAL_RESOURCE_PATH"/ | ||
+ | // info.ssl_private_key_filepath = LOCAL_RESOURCE_PATH"/ | ||
+ | //} | ||
+ | info.gid = -1; | ||
+ | info.uid = -1; | ||
+ | info.options = opts; | ||
+ | | ||
+ | context = lws_create_context(& | ||
+ | | ||
+ | if (context == NULL) { | ||
+ | fprintf(stderr, | ||
+ | return -1; | ||
+ | } | ||
+ | | ||
+ | printf(" | ||
+ | while (1) { | ||
+ | lws_service(context, | ||
+ | } | ||
+ | | ||
+ | lws_context_destroy(context); | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Compilation : | ||
+ | gcc -Wall testServeur.c -o testServeur -lwebsockets | ||
+ | Execution : | ||
+ | ./ | ||
+ | | ||
+ | Créer le fichier index.html avec le contenu suivant : | ||
+ | <code html> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <meta charset=" | ||
+ | <script src=" | ||
+ | </ | ||
+ | <script type=" | ||
+ | $(function() { | ||
+ | window.WebSocket = window.WebSocket || window.MozWebSocket; | ||
+ | var websocket = new WebSocket( | ||
+ | ' | ||
+ | websocket.onopen = function () { | ||
+ | $(' | ||
+ | }; | ||
+ | websocket.onerror = function () { | ||
+ | $(' | ||
+ | }; | ||
+ | websocket.onmessage = function (message) { | ||
+ | console.log(message.data); | ||
+ | $(' | ||
+ | }; | ||
+ | $(' | ||
+ | e.preventDefault(); | ||
+ | websocket.send($(' | ||
+ | $(' | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <input type=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | L' | ||
+ | |||
+ | WebSockets test doit être inscrit en vert ce qui signifie que la connection WebSocket fonctionne bien. | ||
+ | |||
+ | Amusez-vous ! | ||
+ | {{tag> |
serveur_websocket.1561036234.txt.gz · Dernière modification : 2019/06/20 13:10 de Mushussu