PageRenderTime 55ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/tests/transports.xhr-polling.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 139 lines | 121 code | 18 blank | 0 comment | 10 complexity | 03d2f5f38a356f6dfdbf65f2755a7e69 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. var io = require('socket.io')
  2. , http = require('http')
  3. , assert = require('assert')
  4. , querystring = require('querystring')
  5. , port = 7400
  6. , encode = require('socket.io/utils').encode
  7. , decode = require('socket.io/utils').decode
  8. , Polling = require('socket.io/transports/xhr-polling');
  9. function server(callback){
  10. return http.createServer(function(){});
  11. };
  12. function listen(s, callback){
  13. s._port = port;
  14. s.listen(port, callback);
  15. port++;
  16. return s;
  17. };
  18. function client(s){
  19. return http.createClient(s._port, 'localhost');
  20. };
  21. function socket(server, options){
  22. if (!options) options = {};
  23. options.log = false;
  24. if (!options.transportOptions) options.transportOptions = {
  25. 'xhr-polling': {
  26. closeTimeout: 100
  27. }
  28. };
  29. return io.listen(server, options);
  30. };
  31. function get(client, url, callback){
  32. var request = client.request('GET', url, {host: 'localhost'});
  33. request.end();
  34. request.on('response', function(response){
  35. var data = '';
  36. response.setEncoding('utf8');
  37. response.on('data', function(chunk){
  38. data += chunk;
  39. });
  40. response.on('end', function(chunk){
  41. callback(data);
  42. });
  43. });
  44. };
  45. function post(client, url, data, callback){
  46. var query = querystring.stringify(data)
  47. , request = client.request('POST', url, {'Content-Length': Buffer.byteLength(query)});
  48. request.write(query);
  49. request.end();
  50. };
  51. module.exports = {
  52. 'test connection and handshake': function(){
  53. var _server = server()
  54. , _socket = socket(_server)
  55. , trips = 2;
  56. _socket.on('connection', function(client){
  57. assert.ok(client instanceof Polling);
  58. client.on('message', function(msg){
  59. assert.ok(msg == 'from client');
  60. --trips || _server.close();
  61. });
  62. });
  63. listen(_server, function(){
  64. get(client(_server), '/socket.io/xhr-polling', function(data){
  65. var sessid = decode(data);
  66. assert.ok(Object.keys(_socket.clients).length == 1);
  67. assert.ok(sessid == Object.keys(_socket.clients)[0]);
  68. assert.ok(_socket.clients[sessid] instanceof Polling);
  69. _socket.clients[sessid].send('from server');
  70. get(client(_server), '/socket.io/xhr-polling/' + sessid, function(data){
  71. assert.ok(decode(data), 'from server');
  72. --trips || _server.close();
  73. });
  74. post(client(_server), '/socket.io/xhr-polling/' + sessid + '/send', {data: encode('from client')});
  75. });
  76. });
  77. },
  78. 'test clients tracking': function(){
  79. var _server = server()
  80. , _socket = socket(_server);
  81. listen(_server, function(){
  82. get(client(_server), '/socket.io/xhr-polling', function(){
  83. assert.ok(Object.keys(_socket.clients).length == 1);
  84. get(client(_server), '/socket.io/xhr-polling', function(){
  85. assert.ok(Object.keys(_socket.clients).length == 2);
  86. _server.close();
  87. });
  88. });
  89. });
  90. },
  91. 'test buffered messages': function(){
  92. var _server = server()
  93. , _socket = socket(_server, { transportOptions: {
  94. 'xhr-polling': {
  95. closeTimeout: 100,
  96. duration: 200
  97. }
  98. } });
  99. listen(_server, function(){
  100. get(client(_server), '/socket.io/xhr-polling', function(data){
  101. var sessid = decode(data);
  102. assert.ok(_socket.clients[sessid]._open === false);
  103. assert.ok(_socket.clients[sessid].connected);
  104. _socket.clients[sessid].send('from server');
  105. get(client(_server), '/socket.io/xhr-polling/' + sessid, function(data){
  106. var durationCheck;
  107. assert.ok(decode(data) == 'from server');
  108. setTimeout(function(){
  109. assert.ok(_socket.clients[sessid]._open);
  110. assert.ok(_socket.clients[sessid].connected);
  111. durationCheck = true;
  112. }, 100);
  113. get(client(_server), '/socket.io/xhr-polling/' + sessid, function(){
  114. assert.ok(durationCheck);
  115. _server.close();
  116. });
  117. });
  118. });
  119. });
  120. }
  121. };