HTML and CSS Reference
In-Depth Information
Figure 3-8. Remote JavaScript console
Before you build this example, ensure you've built the example in Listing 3-6. If
you've also built the Echo Server piece (Listing 3-7), you'll need to shut down the Echo
Server before testing the ensuing code snippets. Listing 3-8 contains the JavaScript code
for the remote control.
Listing 3-8. websocket-repl.js
var websocket = require("./websocket-example");
var repl = require("repl");
var connections = Object.create(null);
var remoteMultiEval = function(cmd, context, filename, callback) {
for (var c in connections) {
connections[c].send(cmd);
}
callback(null, "(result pending)");
}
websocket.listen(9999, "localhost", function(conn) {
conn.id = Math.random().toString().substr(2);
connections[conn.id] = conn;
console.log("new connection: " + conn.id);
conn.on("data", function(opcode, data) {
console.log("\t" + conn.id + ":\t" + data);
});
conn.on("close", function() {
 
Search WWH ::




Custom Search