Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Forked from cgcardona/gist:525768
Created August 15, 2010 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThisIsMissEm/525784 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/525784 to your computer and use it in GitHub Desktop.
var sys = require("sys")
, fs = require("fs")
, path = require("path")
, http = require("http")
, ws = require('../lib/ws');
var server = http.createServer(function(req, res){
if( req.url.indexOf("favicon") > -1 ){
sys.log("HTTP: "+req.socket.remotePort+", inbound request, served nothing, (favicon)");
res.writeHead(200, {'Content-Type': 'image/x-icon', 'Connection': 'close', 'Content-Length': '0'});
res.end("");
} else {
sys.log("HTTP: "+req.socket.remotePort+", inbound request, served client.html");
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream( path.normalize(path.join(__dirname, "client.html")), {
'flags': 'r',
'encoding': 'binary',
'mode': 0666,
'bufferSize': 4 * 1024
}).addListener("data", function(chunk){
res.write(chunk, 'binary');
}).addListener("close",function() {
res.end();
});
}
}).listen(8000);
var wss = ws.createServer({
debug: true
}, server);
wss.addListener("listening", function(){
sys.log("Listening for connections.");
});
// Handle WebSocket Requests
wss.addListener("connection", function(conn){
sys.log("opened connection: "+conn.id);
conn.addListener("message", function(message){
sys.log("<"+conn.id+"> "+message);
});
});
wss.addListener("close", function(conn){
sys.log("closed connection: "+conn.id);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment