diff options
author | silvioprog <silvioprog@gmail.com> | 2020-04-08 13:51:09 -0300 |
---|---|---|
committer | silvioprog <silvioprog@gmail.com> | 2020-04-08 13:51:09 -0300 |
commit | 2de4ad4cafbe0a017b3494ada2cc430be30fbbf4 (patch) | |
tree | f1f85acc2bc7b65ffd6da5cd9f5219adf8af203d | |
parent | 68200a8426b0b0cd56bae2f737c4ceea1e10d95a (diff) | |
download | libmicrohttpd-2de4ad4cafbe0a017b3494ada2cc430be30fbbf4.tar.gz libmicrohttpd-2de4ad4cafbe0a017b3494ada2cc430be30fbbf4.zip |
Improved websocket example allowing the client to connect to the remote host.
-rw-r--r-- | src/examples/websocket_threaded_example.c | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/examples/websocket_threaded_example.c b/src/examples/websocket_threaded_example.c index 21028901..9133190f 100644 --- a/src/examples/websocket_threaded_example.c +++ b/src/examples/websocket_threaded_example.c | |||
@@ -34,7 +34,7 @@ | |||
34 | "<title>WebSocket chat</title>\n" \ | 34 | "<title>WebSocket chat</title>\n" \ |
35 | "<script>\n" \ | 35 | "<script>\n" \ |
36 | "document.addEventListener('DOMContentLoaded', function() {\n" \ | 36 | "document.addEventListener('DOMContentLoaded', function() {\n" \ |
37 | " const ws = new WebSocket('ws://localhost:%d');\n" \ | 37 | " const ws = new WebSocket('ws://' + window.location.host);\n" \ |
38 | " const btn = document.getElementById('send');\n" \ | 38 | " const btn = document.getElementById('send');\n" \ |
39 | " const msg = document.getElementById('msg');\n" \ | 39 | " const msg = document.getElementById('msg');\n" \ |
40 | " const log = document.getElementById('log');\n" \ | 40 | " const log = document.getElementById('log');\n" \ |
@@ -54,6 +54,7 @@ | |||
54 | " msg.onkeyup = function(ev) {\n" \ | 54 | " msg.onkeyup = function(ev) {\n" \ |
55 | " if (ev.keyCode === 13) {\n" \ | 55 | " if (ev.keyCode === 13) {\n" \ |
56 | " ev.preventDefault();\n" \ | 56 | " ev.preventDefault();\n" \ |
57 | " ev.stopPropagation();\n" \ | ||
57 | " btn.click();\n" \ | 58 | " btn.click();\n" \ |
58 | " msg.value = '';\n" \ | 59 | " msg.value = '';\n" \ |
59 | " }\n" \ | 60 | " }\n" \ |
@@ -91,7 +92,7 @@ | |||
91 | #define WS_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" | 92 | #define WS_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
92 | #define WS_GUID_LEN 36 | 93 | #define WS_GUID_LEN 36 |
93 | #define WS_KEY_LEN 24 | 94 | #define WS_KEY_LEN 24 |
94 | #define WS_KEY_GUI_LEN ((WS_KEY_LEN) + (WS_GUID_LEN)) | 95 | #define WS_KEY_GUID_LEN ((WS_KEY_LEN) + (WS_GUID_LEN)) |
95 | #define WS_FIN 128 | 96 | #define WS_FIN 128 |
96 | #define WS_OPCODE_TEXT_FRAME 1 | 97 | #define WS_OPCODE_TEXT_FRAME 1 |
97 | #define WS_OPCODE_CON_CLOSE_FRAME 8 | 98 | #define WS_OPCODE_CON_CLOSE_FRAME 8 |
@@ -431,12 +432,9 @@ static int | |||
431 | send_chat_page (struct MHD_Connection *con, uint16_t port) | 432 | send_chat_page (struct MHD_Connection *con, uint16_t port) |
432 | { | 433 | { |
433 | struct MHD_Response *res; | 434 | struct MHD_Response *res; |
434 | char page[1024]; | ||
435 | size_t page_len; | ||
436 | int ret; | 435 | int ret; |
437 | page_len = sprintf (page, CHAT_PAGE, port); | 436 | res = MHD_create_response_from_buffer (strlen (CHAT_PAGE), (void *) CHAT_PAGE, |
438 | res = MHD_create_response_from_buffer (page_len, (void *) page, | 437 | MHD_RESPMEM_PERSISTENT); |
439 | MHD_RESPMEM_MUST_COPY); | ||
440 | ret = MHD_queue_response (con, MHD_HTTP_OK, res); | 438 | ret = MHD_queue_response (con, MHD_HTTP_OK, res); |
441 | MHD_destroy_response (res); | 439 | MHD_destroy_response (res); |
442 | return ret; | 440 | return ret; |
@@ -491,7 +489,7 @@ ws_get_accept_value (char *key, unsigned char **val) | |||
491 | strcpy (str, key); | 489 | strcpy (str, key); |
492 | strcat (str, WS_GUID); | 490 | strcat (str, WS_GUID); |
493 | SHA1Reset (&ctx); | 491 | SHA1Reset (&ctx); |
494 | SHA1Input (&ctx, (const uint8_t *) str, WS_KEY_GUI_LEN); | 492 | SHA1Input (&ctx, (const uint8_t *) str, WS_KEY_GUID_LEN); |
495 | SHA1Result (&ctx, hash); | 493 | SHA1Result (&ctx, hash); |
496 | free (str); | 494 | free (str); |
497 | *val = BASE64Encode (hash, SHA1HashSize, NULL); | 495 | *val = BASE64Encode (hash, SHA1HashSize, NULL); |