-
-
Save developerworks/c6237ae66c105326e353b4d775fb25c0 to your computer and use it in GitHub Desktop.
Revisions
-
martinsik revised this gist
Sep 6, 2012 . 2 changed files with 8 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,8 @@ $(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; var websocket = new WebSocket('ws://127.0.0.1:9000', 'dumb-increment-protocol'); websocket.onopen = function () { $('h1').css('color', 'green'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ #include <stdio.h> #include <stdlib.h> #include <libwebsockets.h> @@ -83,7 +82,6 @@ static struct libwebsocket_protocols protocols[] = { } }; int main(void) { // server url will be http://localhost:9000 int port = 9000; @@ -107,16 +105,16 @@ int main(void) { printf("starting server...\n"); // infinite loop, to end this server send SIGTERM. (CTRL+C) while (1) { libwebsocket_service(context, 50); // libwebsocket_service will process all waiting events with their // callback functions and then wait 50 ms. // (this is a single threaded webserver and this will keep our server // from generating load while there are not requests to process) } libwebsocket_context_destroy(context); return 0; } -
martinsik revised this gist
Sep 6, 2012 . 1 changed file with 64 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,49 +1,71 @@ #include <stdio.h> #include <stdlib.h> #include <libwebsockets.h> static int callback_http(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { return 0; } static int callback_dumb_increment(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { switch (reason) { case LWS_CALLBACK_ESTABLISHED: // just log message that someone is connecting printf("connection established\n"); break; case LWS_CALLBACK_RECEIVE: { // the funny part // create a buffer to hold our response // it has to have some pre and post padding. You don't need to care // what comes there, libwebsockets will do everything for you. For more info see // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n597 unsigned char *buf = (unsigned char*) malloc(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING); int i; // pointer to `void *in` holds the incomming request // we're just going to put it in reverse order and put it in `buf` with // correct offset. `len` holds length of the request. for (i=0; i < len; i++) { buf[LWS_SEND_BUFFER_PRE_PADDING + (len - 1) - i ] = ((char *) in)[i]; } // log what we recieved and what we're going to send as a response. // that disco syntax `%.*s` is used to print just a part of our buffer // http://stackoverflow.com/questions/5189071/print-part-of-char-array printf("received data: %s, replying: %.*s\n", (char *) in, (int) len, buf + LWS_SEND_BUFFER_PRE_PADDING); // send response // just notice that we have to tell where exactly our response starts. That's // why there's `buf[LWS_SEND_BUFFER_PRE_PADDING]` and how long it is. // we know that our response has the same length as request because // it's the same message in reverse order. libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], len, LWS_WRITE_TEXT); // release memory back into the wild free(buf); break; } default: break; } return 0; } static struct libwebsocket_protocols protocols[] = { /* first protocol must always be HTTP handler */ { @@ -61,30 +83,40 @@ static struct libwebsocket_protocols protocols[] = { } }; int main(void) { // server url will be http://localhost:9000 int port = 9000; const char *interface = NULL; struct libwebsocket_context *context; // we're not using ssl const char *cert_path = NULL; const char *key_path = NULL; // no special options int opts = 0; // create libwebsocket context representing this server context = libwebsocket_create_context(port, interface, protocols, libwebsocket_internal_extensions, cert_path, key_path, -1, -1, opts); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return -1; } printf("starting server...\n"); // infinite loop, the only option to end this serer is by sending SIGTERM. (CTRL+C) while (1) { libwebsocket_service(context, 50); // libwebsocket_service will process all waiting events with their callback // functions and then wait 50 ms. // (this is single threaded webserver and this will keep // our server from generating load while there are not requests to process) } libwebsocket_context_destroy(context); return 0; } -
martinsik revised this gist
Sep 6, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,6 +26,7 @@ $('button').click(function(e) { e.preventDefault(); websocket.send($('input').val()); $('input').val(''); }); }); </script> -
martinsik revised this gist
Sep 6, 2012 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,10 +30,12 @@ }); </script> </head> <body> <h1>WebSockets test</h1> <form> <input type="text" /> <button>Send</button> </form> <div></div> </body> </html> -
martinsik revised this gist
Sep 6, 2012 . 1 changed file with 1 addition and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <libwebsockets.h> static int callback_http(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, @@ -14,8 +10,6 @@ static int callback_http(struct libwebsocket_context * this, return 0; } static int callback_dumb_increment(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, @@ -50,7 +44,6 @@ static int callback_dumb_increment(struct libwebsocket_context * this, return 0; } static struct libwebsocket_protocols protocols[] = { /* first protocol must always be HTTP handler */ { @@ -62,14 +55,12 @@ static struct libwebsocket_protocols protocols[] = { "dumb-increment-protocol", // protocol name - very important! callback_dumb_increment, // callback 0 // we don't use any per session data }, { NULL, NULL, 0 /* End of list */ } }; int main(void) { int port = 9000; const char *interface = NULL; @@ -96,4 +87,4 @@ int main(void) { libwebsocket_context_destroy(context); return 0; } -
martinsik created this gist
Sep 6, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; var websocket = new WebSocket('ws://127.0.0.1:9000', 'dumb-increment-protocol'); websocket.onopen = function () { $('h1').css('color', 'green'); }; websocket.onerror = function () { $('h1').css('color', 'red'); }; websocket.onmessage = function (message) { console.log(message.data); $('div').append($('<p>', { text: message.data })); }; $('button').click(function(e) { e.preventDefault(); websocket.send($('input').val()); }); }); </script> </head> <body> <h1>Websockets test</h1> <input type="text" /> <button>Send</button> <div></div> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ #include <stdio.h> //#include <string.h> #include <stdlib.h> #include <libwebsockets.h> static int callback_http(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { return 0; } static int callback_dumb_increment(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) { switch (reason) { case LWS_CALLBACK_ESTABLISHED: printf("connection established\n"); 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]; } // http://stackoverflow.com/questions/5189071/print-part-of-char-array printf("received data: %s, replying: %.*s\n", (char *) in, (int) len, buf + LWS_SEND_BUFFER_PRE_PADDING); libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], len, LWS_WRITE_TEXT); free(buf); //libwebsocket_write(wsi, in, len, LWS_WRITE_TEXT); break; } default: break; } return 0; } static struct libwebsocket_protocols protocols[] = { /* first protocol must always be HTTP handler */ { "http-only", // name callback_http, // callback 0 // per_session_data_size }, { "dumb-increment-protocol", // protocol name - very important! callback_dumb_increment, // callback 0 // we don't use any per session data }, { NULL, NULL, 0 /* End of list */ } }; int main(void) { int port = 9000; const char *interface = NULL; struct libwebsocket_context *context; const char *cert_path = NULL; const char *key_path = NULL; int opts = 0; context = libwebsocket_create_context(port, interface, protocols, libwebsocket_internal_extensions, cert_path, key_path, -1, -1, opts); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return -1; } printf("Starting...\n"); while (1) { libwebsocket_service(context, 50); } libwebsocket_context_destroy(context); return 0; }