Index: web/testclient/README =================================================================== --- web/testclient/README (revision 4975) +++ web/testclient/README (revision 4976) @@ -8,13 +8,21 @@ by pressing Ctrl-Shift-C in the browser. -The echo server socket +The server echo socket ---------------------- -The client assumes that there is a websocket server listening at the `/echo` -url that accepts free text messages. The client attempts to open the socket -on startup. +The client assumes that there is a websocket server listening at the +`/serverecho` route that accepts free text messages. +The client attempts to open the socket on startup. -The user can send a message to the echo socket by typing it into the 'message' -text box and clicking the 'send' button. Messages received from the echo -socket are displayed in the 'response' text box. +The user can send a message to the server echo socket by typing it into the +'message' text box and clicking the 'send' button. +Echoes are displayed in the 'response' text box. + + +The client echo socket +---------------------- + +On startup the client opens another socket named `/clientecho`. +When the server sends a message on this socket, the client will faithfully +echo it to the server. Index: web/testclient/index.html =================================================================== --- web/testclient/index.html (revision 4975) +++ web/testclient/index.html (revision 4976) @@ -1,4 +1,4 @@ - + @@ -7,12 +7,13 @@

Websocket test

Press Shift-Ctrl-C to open the debug console. +

Say something to the server

-

say something to the echo

-
+
+

Messages from the server

\ No newline at end of file Index: web/testclient/scripts.js =================================================================== --- web/testclient/scripts.js (revision 4975) +++ web/testclient/scripts.js (revision 4976) @@ -2,9 +2,9 @@ var echoResponse = document.getElementById("echo-response"); -function echoSend() { - console.log(echoText.value); - echoSocket.send(echoText.value); +function sendToEcho() { + console.log("client calls ", echoText.value); + serverEchoSocket.send(echoText.value); echoText.value = ""; } @@ -21,23 +21,40 @@ } -var echoSocket = new WebSocket(getWebSocketUrl("echo"), "remote-hid-protocol"); - -echoSocket.onopen = function (event) { - console.log("echo socket is open at", echoSocket.url); +var serverEchoSocket = + new WebSocket(getWebSocketUrl("serverecho"), "remote-hid-protocol"); +serverEchoSocket.onopen = function (event) { + console.log("server echo socket is connected to", serverEchoSocket.url); }; - -echoSocket.onclose = function (event) { - console.log("echo socket is closed"); +serverEchoSocket.onclose = function (event) { + console.log("server echo socket is closed"); }; - -echoSocket.onerror = function (event) { - console.log("echo socket error", event); +serverEchoSocket.onerror = function (event) { + console.log("server echo socket error", event); }; - -echoSocket.onmessage = function (event) { - console.log(event.data); +serverEchoSocket.onmessage = function (event) { + console.log("server echoes", event.data); echoResponse.value = event.data; }; +var clientEchoSocket = + new WebSocket(getWebSocketUrl("clientecho"), "remote-hid-protocol"); +clientEchoSocket.onopen = function (event) { + console.log("client echo socket is connected to", clientEchoSocket.url); +}; +clientEchoSocket.onclose = function (event) { + console.log("client echo socket is closed"); +}; +clientEchoSocket.onerror = function (event) { + console.log("client echo socket error", event); +}; +clientEchoSocket.onmessage = function (event) { + console.log("client echoes", event.data); + var div = document.createElement("div"); + div.appendChild(document.createTextNode(event.data)); + document.body.appendChild(div); + clientEchoSocket.send(event.data); +}; + + Index: web/testserver/README =================================================================== --- web/testserver/README (revision 4975) +++ web/testserver/README (revision 4976) @@ -4,10 +4,18 @@ Simple node.js webserver for websocket testing. The server serves static files in the ../testclient directory. -There is a websocket server listening at `/echo`, which simply echoes received -messages. +There is a websocket server listening at `/serverecho`, +which simply echoes received messages. +There is another websocket server listening at `/clientecho`. +When the client connects to the socket, the server side user may send text +messages to the client from stdin. +Client responses are logged by the server to stdout together with other +events. + + + Preconditions ------------- Install node.js v4.x.x or higher (v6.x.x is recommended). Index: web/testserver/app.js =================================================================== --- web/testserver/app.js (revision 4975) +++ web/testserver/app.js (revision 4976) @@ -1,3 +1,4 @@ +var readline = require('readline'); var express = require('express'); var morgan = require('morgan'); @@ -4,22 +5,50 @@ var app = express(); var expressWs = require('express-ws')(app); +var serverecho; +var clientecho; + + +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + + +rl.on("line", function (input) { + if (clientecho) { + clientecho.send(input); + } +}); + +// this will serve static files app.use(express.static("../testclient")); + +// this will log http requests app.use(morgan("common")); -app.get('/', function (req, res, next) { - console.log('get route', req.testing); - res.end(); +app.ws('/serverecho', function (ws, req) { + console.log("open server echo socket"); + serverecho = ws; + ws.on("message", function (msg) { + console.log("server echoes", msg); + ws.send(msg); + }); + ws.on("close", function () { + console.log("close server echo socket"); + serverecho = null; + }); }); -app.ws('/echo', function (ws, req) { - console.log("open echo socket"); +app.ws('/clientecho', function (ws, req) { + console.log("open client echo socket"); + clientecho = ws; ws.on("message", function (msg) { - console.log("echo", msg); - ws.send(msg); + console.log("client echoes", msg); }); ws.on("close", function () { - console.log("close echo socket"); + console.log("close client echo socket"); + clientecho = null; }); }); Index: web/testserver/package.json =================================================================== --- web/testserver/package.json (revision 4975) +++ web/testserver/package.json (revision 4976) @@ -5,7 +5,8 @@ "scripts": { "start": "node ./app" }, - "description": "testserver", + "description": "node.js test server for websocket testing", + "license": "MIT", "author": { "name": "Kalman Keri" },