/*--- title: WebSocket echo group: Realtime summary: Upgrade the request and talk to the browser; frames come from the WebSocket window. setup: {"mocks":{"ws":{"refuse":false,"script":[{"afterMs":300,"text":"hello"},{"afterMs":400,"text":"how are you?"},{"afterMs":400,"text":"bye"}],"closeAfterMs":0}}} ---*/ /* While this runs, open the WebSocket tab of the output and type frames yourself. The scripted frames end with "bye"; remove that frame in the WebSocket window to keep the socket open and chat with the script. */ requirelib("websocket"); if (!websocket.upgrade(30)) { sendResp("This was not a WebSocket request"); exit(); } websocket.send("Welcome, " + USERNAME + ". Say bye to leave."); while (true) { var msg = websocket.read(10000); if (msg === null) { websocket.send("Still there?"); continue; } if (msg === false) { console.log("The browser closed the socket"); break; } if (msg === "bye") { websocket.send("Goodbye!"); break; } websocket.send("You said: " + msg.toUpperCase()); } websocket.close();