Websocket: include broadcast demo
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<meta charset="utf-8" />
|
||||
|
||||
<title>WebSocket Test</title>
|
||||
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
var wsUri = "ws://"+window.location.host+"/websocket/ws.cgi";
|
||||
var output;
|
||||
|
||||
function init()
|
||||
{
|
||||
output = document.getElementById("output");
|
||||
testWebSocket();
|
||||
}
|
||||
|
||||
function testWebSocket()
|
||||
{
|
||||
websocket = new WebSocket(wsUri);
|
||||
websocket.onopen = function(evt) { onOpen(evt) };
|
||||
websocket.onclose = function(evt) { onClose(evt) };
|
||||
websocket.onmessage = function(evt) { onMessage(evt) };
|
||||
websocket.onerror = function(evt) { onError(evt) };
|
||||
}
|
||||
|
||||
function onOpen(evt)
|
||||
{
|
||||
writeToScreen("CONNECTED");
|
||||
doSend("WebSocket rocks");
|
||||
}
|
||||
|
||||
function onClose(evt)
|
||||
{
|
||||
writeToScreen("DISCONNECTED");
|
||||
}
|
||||
|
||||
function onMessage(evt)
|
||||
{
|
||||
writeToScreen('<span style="color: blue;">RECEIVED: ' + evt.data+'</span>');
|
||||
// websocket.close();
|
||||
}
|
||||
|
||||
function onError(evt)
|
||||
{
|
||||
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
|
||||
}
|
||||
|
||||
function doSend(message)
|
||||
{
|
||||
writeToScreen("SENT: " + message);
|
||||
websocket.send(message);
|
||||
}
|
||||
|
||||
function writeToScreen(message)
|
||||
{
|
||||
var pre = document.createElement("p");
|
||||
pre.style.wordWrap = "break-word";
|
||||
pre.innerHTML = message;
|
||||
output.appendChild(pre);
|
||||
}
|
||||
|
||||
window.addEventListener("load", init, false);
|
||||
|
||||
</script>
|
||||
|
||||
<h2>WebSocket Test</h2>
|
||||
|
||||
<div id="output"></div>
|
||||
+23
-5
@@ -47,13 +47,28 @@ int myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pas
|
||||
return 0;
|
||||
}
|
||||
|
||||
void myWebsocketRecv(Websock *ws, char *data, int len, int flags) {
|
||||
int i;
|
||||
os_printf("Websocket: Received data: ");
|
||||
for (i=0; i<len; i++) os_printf("%c", data[i]);
|
||||
os_printf("\n");
|
||||
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;
|
||||
char buff[128];
|
||||
os_sprintf(buff, "You sent: ");
|
||||
for (i=0; i<len; i++) buff[i+10]=data[i];
|
||||
buff[i+10]=0;
|
||||
cgiWebsocketSend(ws, buff, os_strlen(buff), WEBSOCK_FLAG_NONE);
|
||||
}
|
||||
|
||||
//Websocket connected. Install reception handler and send welcome message.
|
||||
void myWebsocketConnect(Websock *ws) {
|
||||
ws->recvCb=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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user