diff --git a/html/websocket/index.html b/html/websocket/index.html new file mode 100644 index 0000000..d7423ec --- /dev/null +++ b/html/websocket/index.html @@ -0,0 +1,69 @@ + + + + +WebSocket Test + + + +

WebSocket Test

+ +
diff --git a/user/user_main.c b/user/user_main.c index 5985d50..ad5da04 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -47,13 +47,28 @@ int myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pas return 0; } +static ETSTimer websockTimer; + +//Broadcast the uptime in seconds every second over connected websockets +static void ICACHE_FLASH_ATTR websockTimerCb(void *arg) { + static int ctr=0; + char buff[128]; + ctr++; + os_sprintf(buff, "Up for %d minutes %d seconds!\n", ctr/60, ctr%60); + cgiWebsockBroadcast("/websocket/ws.cgi", buff, os_strlen(buff), WEBSOCK_FLAG_NONE); +} + +//On reception of a message, send "You sent: " plus whatever the other side sent void myWebsocketRecv(Websock *ws, char *data, int len, int flags) { int i; - os_printf("Websocket: Received data: "); - for (i=0; irecvCb=myWebsocketRecv; cgiWebsocketSend(ws, "Hi, Websocket!", 14, WEBSOCK_FLAG_NONE); @@ -150,6 +165,9 @@ void user_init(void) { os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL); os_timer_arm(&prHeapTimer, 3000, 1); #endif + os_timer_disarm(&websockTimer); + os_timer_setfn(&websockTimer, websockTimerCb, NULL); + os_timer_arm(&websockTimer, 1000, 1); os_printf("\nReady\n"); }