HTML and CSS Reference
In-Depth Information
// remove connection
delete connections[conn.id];
});
});
repl.start({"eval": remoteMultiEval});
We will also need a simple web page to control. The script on this page simply opens
a WebSocket to our control server, evaluates any message it receives, and responds with
the result. The client also logs incoming expressions to the JavaScript console. You will
see these expressions if you open your browser's developer tools. Listing 3-9 shows the
web page containing the script.
Listing 3-9. repl-client.html
<!doctype html>
<title>WebSocket REPL Client</title>
<meta charset="utf-8">
<script>
var url = "ws://localhost:9999/repl";
var ws = new WebSocket(url);
ws.onmessage = function(e) {
console.log("command: ", e.data);
try {
var result = eval(e.data);
ws.send(result.toString());
} catch (err) {
ws.send(err.toString());
}
}
</script>
Now if you run node websocket-repl.js , you will see an interactive interpreter.
If you load repl-client.html in a couple of browsers, you will see that each browser
evaluates your commands. Listing 3-10 shows the output for two expressions,
navigator.userAgent and 5+5 .
Listing 3-10. Expression Output from the Console
> new connection: 5206121257506311
new connection: 6689629901666194
navigator.userAgent
'(result pending)'
> 5206121257506311: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0)
Gecko/20100101 Firefox/13.0.1
6689629901666194: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1
(KHTML, like Gecko) Chrome/21.0.1180.15 Safari/537.1
5+5
 
Search WWH ::




Custom Search