HTML and CSS Reference
In-Depth Information
// two bytes extra
b2 |= 126;
buf.writeUInt8(b1, 0);
buf.writeUInt8(b2, 1);
// add two byte length
buf.writeUInt16BE(length, 2);
payload.copy(buf, 4);
} else {
buf = new Buffer(payload.length + 2 + 8);
// eight bytes extra
b2 |= 127;
buf.writeUInt8(b1, 0);
buf.writeUInt8(b2, 1);
// add eight byte length
// note: this implementation cannot handle lengths greater than 2^32
// the 32 bit length is prefixed with 0x0000
buf.writeUInt32BE(0, 2);
buf.writeUInt32BE(length, 6);
payload.copy(buf, 10);
}
return buf;
}
exports.listen = function(port, host, connectionHandler) {
var srv = http.createServer(function(req, res) {
});
srv.on('upgrade', function(req, socket, upgradeHead) {
var ws = new WebSocketConnection(req, socket, upgradeHead);
connectionHandler(ws);
});
srv.listen(port, host);
};
Testing Our Simple WebSocket Server
Now, let's test our server. Echo is the “Hello, World” of networking, so the first thing we
will do with our new server API is create an server, as shown in Listing 3-7. Echo servers
simply respond with whatever the connected client sends. In this case, our WebSocket
echo server will respond with whatever WebSocket messages it receives.
Listing 3-7. Building an Echo Server Using Your New Server API
var websocket = require("./websocket-example");
websocket.listen(9999, "localhost", function(conn) {
console.log("connection opened");
 
Search WWH ::




Custom Search