Created
December 15, 2011 03:30
-
-
Save abarth500/1479703 to your computer and use it in GitHub Desktop.
(Server) WebSocket Chat App. using node.js + websoket modute
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
#!/usr/local/bin/node | |
var conn = []; | |
// https://github.com/Worlize/WebSocket-Node/wiki/Documentation | |
var WebSocketServer = require('websocket').server; | |
var http = require('http'); | |
var originIsAllowed = function(){return true;} | |
var server = http.createServer(function(request, response) { | |
console.log((new Date()) + " Received request for " + request.url); | |
response.writeHead(404); | |
response.end(); | |
}); | |
server.listen(8123, function() { | |
console.log((new Date()) + "Echo Server is listening on port 8123"); | |
}); | |
wsServer = new WebSocketServer({ | |
httpServer: server, | |
autoAcceptConnections: false | |
}); | |
wsServer.on('close', function(request) { | |
console.log("Closed"); | |
}); | |
wsServer.on('request', function(request) { | |
if (!originIsAllowed(request.origin)) { | |
request.reject(); | |
console.log((new Date()) + " Connection from origin " + request.origin + " rejected."); | |
return; | |
} | |
console.log("Connected"); | |
var con = request.accept(null, request.origin) | |
conn.push(con); | |
con.on('message', function(mg) { | |
console.log(mg); | |
for(var c = 0;c < conn.length;c++){ | |
if(!conn[c].closed){ | |
conn[c].sendUTF("\t"+mg.utf8Data+""); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment