-
-
Save ThisIsMissEm/525784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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