Created
August 16, 2010 17:07
Revisions
-
bsstoner revised this gist
Aug 17, 2010 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,6 @@ * }); * */ var SocketManager = exports.SocketManager = function(){ this.socket = null; this.methods = {}; @@ -37,7 +36,6 @@ SocketManager.prototype.register = function(socket, options){ context.connect(client); client.on('message', function(msg){ context.receive(client, msg); }); -
bsstoner revised this gist
Aug 17, 2010 . 1 changed file with 21 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,13 +19,13 @@ * }); * */ var sys = require('sys'); var SocketManager = exports.SocketManager = function(){ this.socket = null; this.methods = {}; this.sessions = {}; this.channels = {}; }; SocketManager.prototype.register = function(socket, options){ var context = this; @@ -37,6 +37,7 @@ SocketManager.prototype.register = function(socket, options){ context.connect(client); client.on('message', function(msg){ sys.log("message: " + msg); context.receive(client, msg); }); @@ -47,11 +48,11 @@ SocketManager.prototype.register = function(socket, options){ }); }; SocketManager.prototype.connect = function(client){ this.sessions[client.sessionId] = client; }; SocketManager.prototype.disconnect = function(client){ if (client.channels && client.channels.length>0){ for (var i=0;i<client.channels.length;i++){ var chn = this.channels[client.channels[i]]; @@ -66,38 +67,42 @@ SocketManager.prototype.disconnect: function(client){ delete this.sessions[client.sessionId]; }; SocketManager.prototype.receive = function(client, msg){ var parsed = JSON.parse(msg); if (parsed.msgType && this.methods[parsed.msgType]){ this.methods[parsed.msgType](client, parsed); } }; SocketManager.prototype.send = function(msgType, sessionId, msgObj){ if (this.sessions[sessionId]){ msgObj.msgType = msgType; this.sessions[sessionId].send(JSON.stringify(msgObj)); } }; SocketManager.prototype.connectToChannel = function(client, channelId){ if (!this.channels[channelId]){ this.channels[channelId] = []; } this.channels[channelId].push(client.sessionId); if (!client.channels){ client.channels = []; } client.channels.push(channelId); }; SocketManager.prototype.broadcastToChannel = function(client, channelId, msgType, msgObj){ if (this.channels[channelId]){ msgObj['msgType'] = msgType; var msg = JSON.stringify(msgObj); for (var i=0;i<this.channels[channelId].length;i++){ var sessionId = this.channels[channelId][i]; if (this.sessions[sessionId]){ this.sessions[sessionId].send(msg); } else { this.channels[channelId].splice(i,1); i--; @@ -106,6 +111,6 @@ SocketManager.prototype.broadcastToChannel(client, channelId, msgType, msgObj){ } }; SocketManager.prototype.on = function(methodName, closure){ this.methods[methodName] = closure; }; -
bsstoner revised this gist
Aug 16, 2010 . 1 changed file with 78 additions and 34 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /** * SocketManager - Singleton to manage multi-channel socket 'routing', need a way to merge with socket.io so client sessions aren't stored twice in memory, * * Requires Socket.IO-node and Socket.IO client libraries. * @@ -20,48 +20,92 @@ * */ exports = function SocketManager(){ this.socket = null; this.messageMethods = {}; this.sessions = {}; this.channels = {}; }(); SocketManager.prototype.register = function(socket, options){ var context = this; this.socket = socket; this.socket.on('connection', function(client){ context.connect(client); client.on('message', function(msg){ context.receive(client, msg); }); client.on('disconnect', function(){ context.disconnect(client); }); }); }; SocketManager.prototype.connect: function(client){ this.sessions[client.sessionId] = client; }; SocketManager.prototype.disconnect: function(client){ if (client.channels && client.channels.length>0){ for (var i=0;i<client.channels.length;i++){ var chn = this.channels[client.channels[i]]; chn.splice(chn.indexOf(client.sessionId),1); // If it was the last one, delete it: if (chn.length==0){ delete this.channels[client.channels[i]]; } } } delete this.sessions[client.sessionId]; }; SocketManager.prototype.receive: function(client, msg){ var parsed = JSON.parse(msg); if (parsed.msgType && this.messageMethods[parsed.msgType]){ this.messageMethods[parsed.msgType](client, parsed); } }; SocketManager.prototype.send: function(msgType, sessionId, msgObj){ if (this.sessions[sessionId]){ msgObj.msgType = msgType; this.sessions[sessionId].send(JSON.stringify(msgObj)); } }; SocketManager.prototype.connectToChannel: function(client, channelId){ if (!this.channels[channelId]){ this.channels[channelId] = []; } this.channels[channelId].push(client.sessionId); client.channels.push(channelId); }; SocketManager.prototype.broadcastToChannel(client, channelId, msgType, msgObj){ if (this.channels[channelId]){ msgObj['msgType'] = msgType; var msg = JSON.stringify(msgObj); for (var i=0;i<this.channels[channelId].length;i++){ var sessionId = this.channels[channelId][i]; if (this.clients[sessionId]){ this.clients[sessionId].send(msg); } else { this.channels[channelId].splice(i,1); i--; } } } }; SocketManager.prototype.on: function(methodName, closure){ this[methodName] = closure; }; -
bsstoner revised this gist
Aug 16, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -58,7 +58,7 @@ exports = function SocketManager(){ if (parsed.msgType && this.messageMethods[parsed.msgType]){ this.messageMethods[parsed.msgType](client, parsed); } }, on: function(methodName, closure){ this[methodName] = closure; -
bsstoner created this gist
Aug 16, 2010 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ /** * SocketManager - Singleton to manage socket 'routing', live (in memory) sessions and message routing * * Requires Socket.IO-node and Socket.IO client libraries. * * Usage: * in your main app.js file (or whereever you create the server) * * var io = require('socket.io'), * sm = require('socketmanager'); * * socket = io.listen(server); // or io.listen(app) in express * * sm.register(socket); * * // Then Register methods that will be run based on the 'msgType' attribute sent from the client in each message * sm.on('joinChat', function(client, messageJSON){ * // do something here (i.e. send message back to client, broadcast something, etc.) * }); * */ exports = function SocketManager(){ socket: null, messageMethods: {}, sessions: {}, register: function(socket){ var context = this; this.socket = socket; this.socket.on('connection', function(client){ context.connect(client); client.on('message', function(msg){ context.receive(client, msg); }); client.on('disconnect', function(){ context.disconnect(client); }); }); }, connect: function(client){ this.sessions[client.sessionId] = client; }, disconnect: function(client){ delete this.sessions[client.sessionId]; }, receive: function(client, msg){ var parsed = JSON.parse(msg); if (parsed.msgType && this.messageMethods[parsed.msgType]){ this.messageMethods[parsed.msgType](client, parsed); } } on: function(methodName, closure){ this[methodName] = closure; } }();