HTML and CSS Reference
In-Depth Information
conn.on("data", function(opcode, data) {
console.log("message: ", data);
conn.send(data);
});
conn.on("close", function(code, reason) {
console.log("connection closed: ", code, reason);
});
});
You can start this server on the command line with node. Make sure websocket-
example.js is in the same directory (or installed as a module).
> node echo.js
If you then connect a WebSocket to this echo server from your browser, you will see
that every message you send from your client is echoed back by the server.
Note When your server listens on localhost, the browser must be on the same machine.
You can also try this out with the Echo client example from Chapter 2.
Building a Remote JavaScript Console
One of the best aspects of JavaScript is how amenable it is to interactive development.
Consoles like those built into the Chrome Developer Tools and Firebug are one of the
reasons JavaScript development can be so productive. A console, also called a REPL
for “Real Eval Print Loop,” lets you enter an expression and see the result. We'll take
advantage of the Node.js repl module and add a custom eval() function. By adding
WebSocket, we can remotely control a web application over the Internet! With this
WebSocket-powered console, we will be able to remotely evaluate expressions from a
command-line interface. Even better, we can enter one expression and see the results of
evaluating that expression for every client concurrently connected.
In this example, you'll use the same server shown in Listing 3-6, then build two small
snippets: one that acts as a remote control and one that acts as the object you control.
Figure 3-8 shows what you'll build in the next example.
 
 
Search WWH ::




Custom Search