Skip to content

Instantly share code, notes, and snippets.

@zztczcx
Last active December 17, 2015 14:39
Show Gist options
  • Save zztczcx/5625591 to your computer and use it in GitHub Desktop.
Save zztczcx/5625591 to your computer and use it in GitHub Desktop.
CONNECT
//master.js
net.createServer(function(socket){
var worker = workers[lastWorkPos++];
if(lastWorkPos >= numCPUs) lastWorkPos = 0;
worker.send({type:'new_client'}, socket, [{ track: false, process: false }]);
}).listen(serverSettings.port);
//child.js
var self = this;
process.on("message", function(msg,socket) {
if(msg && msg.type == 'new_client' && socket) {
var client = mqtt.createConneciton(socket);
client.once('connect',function(packet){
// 将client的引用保存到hash表中
client.device_token = packet.device_token;
self.clients[client.device_token] = client;
});
client.once('close', function(error) {
delete self.clients[client.device_token];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment