Advertisement
Guest User

connectionManager.js

a guest
Oct 12th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var io = require('socket.io').listen(8080, "127.0.0.1");
  2.  
  3. io.configure('production', function(){
  4.     io.enable('browser client minification');  // send minified client
  5.     io.enable('browser client etag');          // apply etag caching logic based on version number
  6.     io.set('log level', 1);                    // reduce logging
  7.     io.set('transports', [                     // enable all transports (optional if you want flashsocket)
  8.         'websocket'
  9.       , 'flashsocket'
  10.       , 'htmlfile'
  11.       , 'jsonp-polling'
  12.     ]);
  13. });
  14.  
  15. var ConnectionManager = function() {
  16.     this._innerSocket = {};
  17.     this._outerSocket = {};
  18.     this.inner = [];
  19.     this.outer = [];
  20.     this.connected = [];
  21. };
  22.  
  23. ConnectionManager.prototype._addSocket = function(type, id, socket) {
  24.     this["_" + type + "Socket"][id] = socket;
  25. };
  26.  
  27. ConnectionManager.prototype.addInner = function(id, socket) {
  28.     if (!~this.inner.indexOf(id)) {
  29.         this._addSocket("inner", id, socket);
  30.         this.inner.push(id);
  31.         console.log("addInner", id);
  32.         if (~this.outer.indexOf(id)) {
  33.             this.connect(id);
  34.         }
  35.     }
  36. };
  37.  
  38. ConnectionManager.prototype.addOuter = function(id, socket) {
  39.     if (!~this.outer.indexOf(id)) {
  40.         this._addSocket("outer", id, socket);
  41.         this.outer.push(id);
  42.         console.log("addOuter", id);
  43.         if (~this.inner.indexOf(id)) {
  44.             this.connect(id);
  45.         }
  46.     }
  47. };
  48.  
  49. ConnectionManager.prototype.connect = function(id) {
  50.     var innerSocket, outerSocket, that;
  51.    
  52.     console.log("connect", id)
  53.    
  54.     that = this;
  55.    
  56.     if (!~this.connected.indexOf(id)) {
  57.         innerSocket = this._innerSocket[id];
  58.         outerSocket = this._outerSocket[id];
  59.         innerSocket.emit("connectedID", id);
  60.         outerSocket.emit("connectedID", id);
  61.        
  62.         innerSocket.on("openOverlay", function(){
  63.             outerSocket.emit("openOverlay");
  64.         });
  65.        
  66.         innerSocket.on("disconnect", function(){
  67.             console.log("disconnected inner", id);
  68.             that.disconnect(id);
  69.             that.addOuter(id, outerSocket);
  70.         });
  71.        
  72.         outerSocket.on("disconnect", function(){
  73.             console.log("disconnected outer", id);
  74.             that.disconnect(id);
  75.             that.addInner(id, innerSocket);
  76.         });
  77.     } else {
  78.         console.log("Connection for " + id + " refused because its already existent.");
  79.     }
  80. };
  81.  
  82. ConnectionManager.prototype.disconnect = function(id) {
  83.     var innerSocket, outerSocket;
  84.    
  85.     if (!~this.connected.indexOf(id)) {
  86.         this.connected.splice(this.connected.indexOf(id), 1);
  87.         this.inner.splice(this.inner.indexOf(id), 1);
  88.         this.outer.splice(this.outer.indexOf(id), 1);
  89.         console.log("disconnected", id)
  90.     }
  91. };
  92.  
  93. var connectionManager = new ConnectionManager();
  94.  
  95. io.sockets.on('connection', function (socket) {
  96.    
  97.     socket.on("registerInner", function(data){
  98.         connectionManager.addInner(data.id, socket);
  99.     });
  100.    
  101.     socket.on("registerOuter", function(data){
  102.         connectionManager.addOuter(data.id, socket);
  103.     });
  104.    
  105. });
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement