Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. bsstoner revised this gist Aug 17, 2010. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions node-socket.io a node.js SocketManager Object
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,6 @@
    * });
    *
    */
    var sys = require('sys');
    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){
    sys.log("message: " + msg);
    context.receive(client, msg);
    });

  2. bsstoner revised this gist Aug 17, 2010. 1 changed file with 21 additions and 16 deletions.
    37 changes: 21 additions & 16 deletions node-socket.io a node.js SocketManager Object
    Original file line number Diff line number Diff line change
    @@ -19,13 +19,13 @@
    * });
    *
    */
    exports = function SocketManager(){
    var sys = require('sys');
    var SocketManager = exports.SocketManager = function(){
    this.socket = null;
    this.messageMethods = {};
    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){
    SocketManager.prototype.connect = function(client){
    this.sessions[client.sessionId] = client;
    };

    SocketManager.prototype.disconnect: function(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){
    SocketManager.prototype.receive = function(client, msg){
    var parsed = JSON.parse(msg);
    if (parsed.msgType && this.messageMethods[parsed.msgType]){
    this.messageMethods[parsed.msgType](client, parsed);
    if (parsed.msgType && this.methods[parsed.msgType]){
    this.methods[parsed.msgType](client, parsed);
    }
    };

    SocketManager.prototype.send: function(msgType, sessionId, msgObj){
    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){
    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(client, channelId, msgType, msgObj){
    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.clients[sessionId]){
    this.clients[sessionId].send(msg);
    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[methodName] = closure;
    SocketManager.prototype.on = function(methodName, closure){
    this.methods[methodName] = closure;
    };
  3. bsstoner revised this gist Aug 16, 2010. 1 changed file with 78 additions and 34 deletions.
    112 changes: 78 additions & 34 deletions node-socket.io a node.js SocketManager Object
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * SocketManager - Singleton to manage socket 'routing', live (in memory) sessions and message routing
    * 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 = {};
    }();

    socket: null,
    messageMethods: {},
    sessions: {},

    register: function(socket){
    var context = this;

    this.socket = socket;
    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);
    });

    this.socket.on('connection', function(client){
    client.on('disconnect', function(){
    context.disconnect(client);
    });

    context.connect(client);
    });
    };

    SocketManager.prototype.connect: function(client){
    this.sessions[client.sessionId] = client;
    };

    client.on('message', function(msg){
    context.receive(client, msg);
    });

    client.on('disconnect', function(){
    context.disconnect(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] = [];
    }

    connect: function(client){
    this.sessions[client.sessionId] = client;
    },
    this.channels[channelId].push(client.sessionId);
    client.channels.push(channelId);
    };

    disconnect: function(client){
    delete this.sessions[client.sessionId];
    },
    SocketManager.prototype.broadcastToChannel(client, channelId, msgType, msgObj){
    if (this.channels[channelId]){
    msgObj['msgType'] = msgType;
    var msg = JSON.stringify(msgObj);

    receive: function(client, msg){
    var parsed = JSON.parse(msg);
    if (parsed.msgType && this.messageMethods[parsed.msgType]){
    this.messageMethods[parsed.msgType](client, parsed);
    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--;
    }
    }
    },

    on: function(methodName, closure){
    this[methodName] = closure;
    }
    };

    }();
    SocketManager.prototype.on: function(methodName, closure){
    this[methodName] = closure;
    };
  4. bsstoner revised this gist Aug 16, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion node-socket.io a node.js SocketManager Object
    Original 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;
  5. bsstoner created this gist Aug 16, 2010.
    67 changes: 67 additions & 0 deletions node-socket.io a node.js SocketManager Object
    Original 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;
    }

    }();