diff --git a/test-server/test-server.c b/test-server/test-server.c index 50a49758..d35e826b 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -27,6 +27,20 @@ #include "../lib/libwebsockets.h" +/* + * This demo server shows how to use libwebsockets for one or more + * websocket protocols in the same server + * + * It defines the following websocket protocols: + * + * dumb-increment-protocol: once the socket is opened, an incrementing + * ascii string is sent down it every 50ms. + * If you send "reset\n" on the websocket, then + * the incrementing number is reset to 0. + * + */ + + #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server" static int port = 7681; static int use_ssl = 0; @@ -95,6 +109,7 @@ callback_dumb_increment(struct libwebsocket * wsi, break; case LWS_CALLBACK_RECEIVE: + fprintf(stderr, "rx %d\n", len); if (len < 6) break; if (strcmp(in, "reset\n") == 0) @@ -109,6 +124,91 @@ callback_dumb_increment(struct libwebsocket * wsi, } +/* lws-mirror_protocol */ + +#define MAX_MESSAGE_QUEUE 64 +const int MAX_COMMUNE_MEMBERS = 20; + +struct per_session_data__lws_mirror { + struct libwebsocket * wsi; + int ringbuffer_tail; +}; + +struct a_message { + struct per_session_data * sender; + void * payload; + size_t len; +}; + +static struct a_message ringbuffer[MAX_MESSAGE_QUEUE]; +static int ringbuffer_head; + + +struct per_session_data * all_members; + + +static int +callback_lws_mirror(struct libwebsocket * wsi, + enum libwebsocket_callback_reasons reason, + void * user, void *in, size_t len) +{ + int n; + char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + + LWS_SEND_BUFFER_POST_PADDING]; + unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING]; + struct per_session_data__lws_mirror * pss = user; + + switch (reason) { + + case LWS_CALLBACK_ESTABLISHED: + pss->wsi = wsi; + pss->ringbuffer_tail = ringbuffer_head; + break; + + case LWS_CALLBACK_SEND: + /* send everything that's pending */ + while (pss->ringbuffer_tail != ringbuffer_head) { + + n = libwebsocket_write(wsi, + (unsigned char *)ringbuffer[pss->ringbuffer_tail].payload + + LWS_SEND_BUFFER_PRE_PADDING, + ringbuffer[pss->ringbuffer_tail].len, + LWS_WRITE_TEXT); + if (n < 0) { + fprintf(stderr, "ERROR writing to socket"); + exit(1); + } + + if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1)) + pss->ringbuffer_tail = 0; + else + pss->ringbuffer_tail++; + } + break; + + case LWS_CALLBACK_RECEIVE: +// fprintf(stderr, "Received %d bytes payload\n", (int)len); + ringbuffer[ringbuffer_head].payload = + malloc(LWS_SEND_BUFFER_PRE_PADDING + len + + LWS_SEND_BUFFER_POST_PADDING); + ringbuffer[ringbuffer_head].len = len; + ringbuffer[ringbuffer_head].sender = pss; + memcpy(ringbuffer[ringbuffer_head].payload + + LWS_SEND_BUFFER_PRE_PADDING, in, len); + if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1)) + ringbuffer_head = 0; + else + ringbuffer_head++; + break; + + default: + break; + } + + return 0; +} + + /* list of supported protocols and callbacks */ static const struct libwebsocket_protocols protocols[] = { @@ -123,6 +223,12 @@ static const struct libwebsocket_protocols protocols[] = { .per_session_data_size = sizeof(struct per_session_data__dumb_increment), }, + { + .name = "lws-mirror-protocol", + .callback = callback_lws_mirror, + .per_session_data_size = + sizeof(struct per_session_data__lws_mirror), + }, { /* end of list */ .callback = NULL } diff --git a/test-server/test.html b/test-server/test.html index 15392092..e2c9934f 100644 --- a/test-server/test.html +++ b/test-server/test.html @@ -15,48 +15,161 @@ reset the number.

-
Not initialized
+
Not initialized
+ + + +

libwebsockets "lws-mirror-protocol" test applet

+Use the mouse to draw on the canvas below -- all other browser windows open +on this page see your drawing in realtime and you can see any of theirs as +well. +

+The lws-mirror protocol doesn't interpret what is being sent to it, it just +re-sends it to every other websocket it has a connection with using that +protocol, including the guy who sent the packet. +

+ + + + + + + +
Drawing color: + +
Not initialized