Skip to content

Instantly share code, notes, and snippets.

@agileguy
Created April 8, 2011 20:15
Show Gist options
  • Select an option

  • Save agileguy/910613 to your computer and use it in GitHub Desktop.

Select an option

Save agileguy/910613 to your computer and use it in GitHub Desktop.
My first node.js socket server
require('datejs');
require('sys');
var net = require('net');
var sockets = new Array();
var server = net.createServer();
server.once('connection', function () {
setInterval(function(){
var a = Date.today().setTimeToNow();
for(i = sockets.length; i > -1; i--){
try
{
sockets[i].write(a.toString() + ' no of clients:' + sockets.length + '\n');
}
catch(err)
{
if (typeof sockets[i] != 'undefined')
sockets.splice(i, 1);
}
}
}, 2000);
})
server.on('connection', function(socket){
sockets.push(socket);
})
server.listen(8123);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment