PageRenderTime 84ms CodeModel.GetById 41ms app.highlight 17ms RepoModel.GetById 23ms app.codeStats 0ms

/js/lib/Socket.IO-node/tests/transports.websocket.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 246 lines | 213 code | 33 blank | 0 comment | 38 complexity | 7d28ff78a8fce4eab17c8a1d7c1c38d9 MD5 | raw file
  1var io = require('socket.io')
  2  , assert = require('assert')
  3  , encode = require('socket.io/utils').encode
  4  , decode = require('socket.io/utils').decode
  5  , port = 7200
  6  , Listener = io.Listener
  7  , Client = require('socket.io/client')
  8  , WebSocket = require('./../support/node-websocket-client/lib/websocket').WebSocket;
  9
 10function server(){
 11  return require('http').createServer(function(){});
 12};
 13
 14function socket(server, options){
 15  if (!options) options = {};
 16  options.log = false;
 17  return io.listen(server, options);
 18};
 19
 20function listen(s, callback){
 21  s._port = port;
 22  s.listen(port, callback);
 23  port++;
 24  return s;
 25};
 26
 27function client(server, sessid){
 28  sessid = sessid ? '/' + sessid : '';
 29  return new WebSocket('ws://localhost:' + server._port + '/socket.io/websocket' + sessid, 'borf');
 30};
 31
 32module.exports = {
 33  
 34  'test connection and handshake': function(){
 35    var _server = server()
 36      , _socket = socket(_server)
 37      , _client
 38      , trips = 2;
 39    
 40    function close(){
 41      _client.close();
 42      _server.close();
 43    };
 44    
 45    listen(_server, function(){
 46      var messages = 0;
 47      _client = client(_server);
 48      _client.onopen = function(){
 49        _client.send(encode('from client'));
 50      };
 51      _client.onmessage = function(ev){
 52        if (++messages == 2){ // first message is the session id
 53          assert.ok(decode(ev.data), 'from server');
 54          --trips || close();
 55        }
 56      };
 57    });
 58    
 59    _socket.on('connection', function(conn){
 60      assert.ok(conn instanceof Client);
 61      conn
 62        .on('message', function(msg){
 63          assert.ok(msg == 'from client');
 64          --trips || close();
 65        })
 66        .send('from server');
 67    });
 68  },
 69  
 70  'test clients tracking': function(){
 71    var _server = server()
 72      , _socket = socket(_server);
 73      
 74    listen(_server, function(){
 75      var _client = client(_server);
 76      _client.onopen = function(){
 77        assert.ok(Object.keys(_socket.clients).length == 1);
 78  
 79        var _client2 = client(_server);
 80        _client2.onopen = function(){
 81          assert.ok(Object.keys(_socket.clients).length == 2);
 82          
 83          _client.close();
 84          _client2.close();
 85          _server.close();
 86        };
 87      }
 88    });
 89  },
 90  
 91  'test buffered messages': function(){
 92    var _server = server()
 93      , _socket = socket(_server, {
 94          transportOptions: {
 95            websocket: {
 96              closeTimeout: 5000
 97            }
 98          }
 99        });
100      
101    listen(_server, function(){
102      var _client = client(_server);
103      
104      _client.onopen = function(){
105        assert.ok(Object.keys(_socket.clients).length == 1);
106        var sessionid = Object.keys(_socket.clients)[0]
107          , runOnce = false;
108  
109        _socket.clients[sessionid].connection.addListener('end', function(){
110          if (!runOnce){
111            assert.ok(_socket.clients[sessionid]._open == false);
112            assert.ok(_socket.clients[sessionid].connected);
113            _socket.clients[sessionid].send('should get this');
114  
115            var _client2 = client(_server, sessionid);
116            _client2.onmessage = function(ev){
117              assert.ok(Object.keys(_socket.clients).length == 1);
118              assert.ok(decode(ev.data), 'should get this');
119              _socket.clients[sessionid].closeTimeout = 0;
120              _client2.close();
121              _server.close();
122            };
123            runOnce = true;
124          }
125        });
126        
127        _client.close();
128      };
129    });
130  },
131  
132  'test json encoding': function(){
133    var _server = server()
134      , _socket = socket(_server)
135      , _client
136      , trips = 2;
137    
138    function close(){
139      _server.close();
140      _client.close();
141    };
142    
143    listen(_server, function(){
144      _socket.on('connection', function(conn){
145        conn.on('message', function(msg){
146          assert.ok(msg.from == 'client');
147          --trips || close();
148        });
149        conn.send({ from: 'server' });
150      });
151      
152      var messages = 0;
153      
154      _client = client(_server);
155      _client.onmessage = function(ev){
156        if (++messages == 2){
157          assert.ok(decode(ev.data)[0].substr(0, 3) == '~j~');
158          assert.ok(JSON.parse(decode(ev.data)[0].substr(3)).from == 'server');
159          _client.send(encode({ from: 'client' }));
160          --trips || close();
161        }
162      };
163    });
164  },
165  
166  'test hearbeat timeout': function(){
167    var _server = server()
168      , _socket = socket(_server, {
169          transportOptions: {
170            websocket: {
171              timeout: 100,
172              heartbeatInterval: 1
173            }
174          }
175        });
176    
177    listen(_server, function(){
178      var _client = client(_server)
179        , messages = 0;
180      _client.onmessage = function(ev){
181        ++messages;
182        if (decode(ev.data)[0].substr(0, 3) == '~h~'){
183          assert.ok(messages === 2);
184          assert.ok(Object.keys(_socket.clients).length == 1);
185          setTimeout(function(){
186            assert.ok(Object.keys(_socket.clients).length == 0);
187            _client.close();
188            _server.close();
189          }, 150);
190        }
191      };
192    });
193  },
194  
195  'test client broadcast': function(){
196    var _server = server()
197      , _socket = socket(_server);
198    
199    listen(_server, function(){
200      var _client = client(_server)
201        , _client2
202        , _client3
203        , _first
204        , _connections = 0;
205        
206      _client.onmessage = function(ev){
207        if (!('messages' in _client)) _client.messages = 0;
208        if (++_client.messages == 2){
209          assert.ok(decode(ev.data)[0] == 'not broadcasted');
210          _client.close();
211          _client2.close();
212          _client3.close();
213          _server.close();
214        }
215      };
216      
217      _client.onopen = function(){
218        _client2 = client(_server);
219        _client2.onmessage = function(ev){
220          if (!('messages' in _client2)) _client2.messages = 0;
221          if (++_client2.messages == 2)
222            assert.ok(decode(ev.data)[0] == 'broadcasted')
223        };
224        _client2.onopen = function(){
225          _client3 = client(_server);
226          _client3.onmessage = function(ev){
227            if (!('messages' in _client3)) _client3.messages = 0;
228            if (++_client3.messages == 2)
229              assert.ok(decode(ev.data)[0] == 'broadcasted')
230          };
231        };
232      };
233      
234      _socket.on('connection', function(conn){
235        if (!_first)
236          _first = conn;
237        if (++_connections == 3){
238          _first.broadcast('broadcasted');
239          _first.send('not broadcasted');
240        }
241      });
242      
243    });
244  }
245  
246};