Skip to content

Commit b7d5aea

Browse files
committed
Use bin2hex to bundle the HTML contents in the websockets sample
1 parent 39b3711 commit b7d5aea

File tree

3 files changed

+19
-109
lines changed

3 files changed

+19
-109
lines changed

src/samples/websocket/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,17 @@ target_link_libraries(websocket
66
${LWAN_COMMON_LIBS}
77
${ADDITIONAL_LIBRARIES}
88
)
9+
10+
add_custom_command(
11+
OUTPUT ${CMAKE_BINARY_DIR}/websocket-sample.h
12+
COMMAND bin2hex
13+
${CMAKE_SOURCE_DIR}/src/samples/websocket/index.html index_html > ${CMAKE_BINARY_DIR}/websocket-sample.h
14+
DEPENDS ${CMAKE_SOURCE_DIR}/src/samples/websocket/index.html
15+
bin2hex
16+
COMMENT "Bundling websocket sample index"
17+
)
18+
add_custom_target(generate_websocket_sample
19+
DEPENDS ${CMAKE_BINARY_DIR}/websocket-sample.h
20+
)
21+
22+
add_dependencies(websocket generate_websocket_sample)

src/samples/websocket/index.html

3.56 KB
Binary file not shown.

src/samples/websocket/main.c

+5-109
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "lwan.h"
2525
#include "lwan-pubsub.h"
2626

27+
#include "websocket-sample.h"
28+
2729
static struct lwan_pubsub_topic *chat;
2830

2931
/* This is a write-only sample of the API: it just sends random integers
@@ -197,116 +199,10 @@ LWAN_HANDLER_ROUTE(ws_chat, "/ws-chat")
197199

198200
LWAN_HANDLER_ROUTE(index, "/")
199201
{
200-
static const char message[] =
201-
"<html>\n"
202-
" <head>\n"
203-
" <script type=\"text/javascript\">\n"
204-
" write_sock = new "
205-
"WebSocket(\"ws://localhost:8080/ws-write\")\n"
206-
" write_sock.onmessage = function (event) {\n"
207-
" document.getElementById(\"write-output\").innerText = "
208-
"event.data;\n"
209-
" }\n"
210-
" write_sock.onerror = function(event) {\n"
211-
" document.getElementById(\"write-output\").innerText = "
212-
"\"Disconnected\";\n"
213-
" "
214-
"document.getElementById(\"write-output\").style.background = "
215-
"\"red\";\n"
216-
" }\n"
217-
" write_sock.onopen = function(event) {\n"
218-
" "
219-
"document.getElementById(\"write-output\").style.background = "
220-
"\"blue\";\n"
221-
" }\n"
222-
" read_sock = new "
223-
"WebSocket(\"ws://localhost:8080/ws-read\")\n"
224-
" read_sock.onmessage = function (event) {\n"
225-
" document.getElementById(\"read-output\").innerText = "
226-
"event.data;\n"
227-
" }\n"
228-
" read_sock.onopen = function(event) {\n"
229-
" document.getElementById(\"read-button\").disabled = "
230-
"false;\n"
231-
" document.getElementById(\"read-input\").disabled = "
232-
"false;\n"
233-
" "
234-
"document.getElementById(\"read-output\").style.background = "
235-
"\"blue\";\n"
236-
" document.getElementById(\"read-output\").innerText = "
237-
"\"\";\n"
238-
" }\n"
239-
" read_sock.onerror = function(event) {\n"
240-
" document.getElementById(\"read-button\").disabled = "
241-
"true;\n"
242-
" document.getElementById(\"read-input\").disabled = "
243-
"true;\n"
244-
" document.getElementById(\"read-output\").innerText = "
245-
"\"Disconnected\";\n"
246-
" "
247-
"document.getElementById(\"read-output\").style.background = \"red\";\n"
248-
" }\n"
249-
" send_to_read_sock = function() {\n"
250-
" "
251-
"read_sock.send(document.getElementById(\"read-input\").value);\n"
252-
" }\n"
253-
" chat_sock = new "
254-
"WebSocket(\"ws://localhost:8080/ws-chat\")\n"
255-
" chat_sock.onopen = function(event) {\n"
256-
" document.getElementById(\"chat-button\").disabled = "
257-
"false;\n"
258-
" document.getElementById(\"chat-input\").disabled = "
259-
"false;\n"
260-
" "
261-
"document.getElementById(\"chat-textarea\").style.background = "
262-
"\"blue\";\n"
263-
" document.getElementById(\"chat-input\").innerText = "
264-
"\"\";\n"
265-
" }\n"
266-
" chat_sock.onerror = function(event) {\n"
267-
" document.getElementById(\"chat-button\").disabled = "
268-
"true;\n"
269-
" document.getElementById(\"chat-input\").disabled = "
270-
"true;\n"
271-
" document.getElementById(\"chat-input\").innerText = "
272-
"\"Disconnected\";\n"
273-
" "
274-
"document.getElementById(\"chat-textarea\").style.background = "
275-
"\"red\";\n"
276-
" }\n"
277-
" chat_sock.onmessage = function (event) {\n"
278-
" document.getElementById(\"chat-textarea\").value += "
279-
"event.data;\n"
280-
" }\n"
281-
" send_chat_msg = function() {\n"
282-
" "
283-
"chat_sock.send(document.getElementById(\"chat-input\").value);\n"
284-
" document.getElementById(\"chat-input\").value = \"\";\n"
285-
" }\n"
286-
" </script>\n"
287-
" </head>\n"
288-
" <body>\n"
289-
" <h1>Lwan WebSocket demo!</h1>\n"
290-
" <h2>Send-only sample: server is writing this "
291-
"continuously:</h2>\n"
292-
" <p><div id=\"write-output\" style=\"background: red; color: "
293-
"yellow\">Disconnected</div></p>\n"
294-
" <h2>Echo server sample:</h2>\n"
295-
" <p><input id=\"read-input\" disabled><button disabled "
296-
"id=\"read-button\" onclick=\"send_to_read_sock()\">Send</button></p>\n"
297-
" <p>Server said this: <div id=\"read-output\" "
298-
"style=\"background: red; color: yellow\">Disconnected</div></p>\n"
299-
" <h3>Chat sample:</h3>\n"
300-
" Send message: <input id=\"chat-input\" disabled><button "
301-
"disabled id=\"chat-button\" "
302-
"onclick=\"send_chat_msg()\">Send</button></p>\n"
303-
" <textarea id=\"chat-textarea\" rows=\"20\" cols=\"120\" "
304-
"style=\"color: yellow; background-color: red\"></textarea>\n"
305-
" </body>\n"
306-
"</html>";
307-
308202
request->response.mime_type = "text/html";
309-
lwan_strbuf_set_static(response->buffer, message, sizeof(message) - 1);
203+
lwan_strbuf_set_static(response->buffer,
204+
index_html_value.value,
205+
index_html_value.len);
310206

311207
return HTTP_OK;
312208
}

0 commit comments

Comments
 (0)