static O eleu_webSocket_onMessage; svoid eleu_webSocket_init { if (webServerPort != 0) pcall { //serveHttp(webServerPort); serveHttpWithWebsockets(webServerPort, func(NanoHTTPD.IHTTPSession handshake) { WebSocket ws = new WebSocket(handshake) { L msgs = synchroList(); volatile long lastMessage = sysNow(); protected void onPong(WebSocketFrame pongFrame) { // don't use WebSocket ping/pong } protected void onMessage(WebSocketFrame messageFrame) { //print("websocket msg: " + messageFrame.getTextPayload()); lastMessage = sysNow(); S s = messageFrame.getTextPayload(); msgs.add(s); if (eq(s, "ping")) incAtomicLong(webSocketPongs); pcallF(eleu_webSocket_onMessage, this, s); } protected void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) { //print("websocket close"); webSockets.remove(this); } protected void onException(IOException e) { printStackTrace(e); } void clean() { if (sysNow() >= lastMessage + webSocketTimeOut) { webSockets.remove(this); pcall { close(WebSocketFrame.CloseCode.NormalClosure, "timeout"); } } } }; webSockets.add(ws); ret ws; }); }