Created
June 23, 2011 17:09
-
-
Save schamane/1043010 to your computer and use it in GitHub Desktop.
nodejs_net_server-client-publisher
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 net = require('net'), | |
events = require('events'), | |
util = require('util'), | |
port = 8124, | |
localhost = "127.0.0.1", | |
Server, Client; | |
Client = function(id, socket) { | |
Client.super_.call(this); | |
this.id = id; | |
this.socket = socket; | |
this.socket.on('data', this.gotData.bind(this)); | |
this.socket.on('end', this.closeClient.bind(this)); | |
}; | |
util.inherits(Client, events.EventEmitter); | |
Client.prototype.gotData = function(data){ | |
console.log("server received message from client" + this.id +":" + data.toString()); | |
this.emit("message", this.id, data); | |
//inform client about published message | |
this.socket.write("your message to be send to all connected clients."); | |
}; | |
Client.prototype.closeClient = function(){ | |
this.emit('close', this.id); | |
this.socket.end(); | |
}; | |
Server = function() { | |
Server.super_.call(this, this.onClientConnect); | |
this.clients = []; | |
}; | |
util.inherits(Server, net.Server); | |
Server.prototype.onClientConnect = function(socket) { | |
var client = new Client(this.connections, socket); | |
this.clients[client.id] = client; | |
client.on('message', this.publishMessage.bind(this)); | |
client.on('close', this.removeClient.bind(this)); | |
}; | |
Server._procMessage = function(i, id, clients, msg) { | |
var client = clients[i]; | |
if(client && client.id !== id) { | |
client.socket.write(msg); | |
} | |
i++; | |
if(i < clients.length) | |
process.nextTick(function() { Server._procMessage(i, id, clients, msg); }); | |
}; | |
/** | |
* send message to whole list of connected clients async | |
*/ | |
Server.prototype.publishMessage = function(id, msg){ | |
Server._procMessage(0, id, this.clients, msg); | |
}; | |
Server.prototype.removeClient = function(id){ | |
console.log("server remove client"+id); | |
delete this.clients[id]; | |
}; | |
new Server().listen(port, localhost, function(){ | |
var testClient1 = net.createConnection(port, localhost), | |
testClient2 = net.createConnection(port, localhost); | |
testClient1.on('connect', function() { | |
console.log("client1 connected"); | |
setTimeout(function(){ | |
testClient1.write("message from first client"); | |
}, 1000); | |
}); | |
testClient1.on('data', function(data){ | |
console.log("client1 received:" + data); | |
}); | |
testClient2.on('connect', function() { | |
console.log("client2 connected"); | |
}); | |
testClient2.on('data', function(data){ | |
console.log("client2 received:" + data); | |
}); | |
setTimeout(function() { | |
testClient1.end(); | |
testClient2.end(); | |
}, 3000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment