!7 static int port; static O onUtterance; // voidfunc(S) static L webSockets = synchroList(); p { NanoHTTPD.SOCKET_READ_TIMEOUT = 10*60*1000; // 10 minutes - TODO: Fix in NanoHTTPD port = serveHttpWithWebSockets(9999, func(NanoHTTPD.IHTTPSession handshake) { ret addAndReturn(webSockets, new WebSocket(handshake) { protected void onPong(WebSocketFrame pongFrame) { print("pong"); } protected void onMessage(WebSocketFrame messageFrame) { S s = messageFrame.getTextPayload(); print("User said: " + s); pcallF(onUtterance, s); } protected void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) { webSockets.remove(this); } protected void onException(IOException e) { printStackTrace(e); } }); }); S url = print("http://localhost:" + port); openPlatformBrowser(url); } html { if (neq(uri, "/popup")) ret hbody("Opening popup..." + hjavascript([[ window.open('/popup', 'speech_recognizer', 'width=300,height=300,location=no'); setTimeout(function() { window.close(); }, 10000); ]])); ret h3("Speech Recognizer") + loadJQuery() + hdiv("Results come here", id := 'results, style := "margin: 10px") + hjavascript([[ var websocket = new WebSocket("ws://localhost:#PORT#/"); websocket.onopen = function(event) { $("#btn").prop('disabled', false); }; var recognition = new webkitSpeechRecognition(); recognition.lang = "en-US"; recognition.onerror = function(event) { $("#results").html("Error."); started = false; $("#btn").html("Start recognition"); } recognition.onresult = function(event) { var result = event.results[0]; var transcript = result[0].transcript; $("#results").html("Transcript: " + transcript + (websocket ? " - have websocket" : "")); websocket.send(transcript); started = false; $("#btn").html("Start recognition"); } var started = false; function startOrStop() { if (started) { recognition.stop(); started = false; $("#btn").html("Start recognition"); } else { recognition.start(); started = true; $("#btn").html("Stop recognition"); } } ]]).replace("#PORT#", str(port)) + tag('button, "Start recognition", onclick := "startOrStop()", type := 'button, id := 'btn, disabled := 'disabled); //+ p(ahref("#", "Popup", onClick := "window.open('/', 'speech_recognizer', 'width=300,height=300,location=no'); return false;")); } svoid startRecognition { for (WebSocket ws : webSockets) ws.send("start"); }