app.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * Module dependencies.
  3. */
  4. var express = require('express')
  5. , stylus = require('stylus')
  6. , sio = require('socket.io')
  7. , path = require('path')
  8. , fs = require('fs');
  9. /**
  10. * App.
  11. */
  12. var app = express.createServer();
  13. /**
  14. * Initial port to listen to.
  15. */
  16. var port = 3000;
  17. /**
  18. * Transport to test with.
  19. */
  20. var args = process.argv.slice(2)
  21. , transport = args.length ? args[0] : 'xhr-polling';
  22. /**
  23. * A map of tests to socket.io ports we're listening on.
  24. */
  25. var testsPorts = {};
  26. /**
  27. * App configuration.
  28. */
  29. app.configure(function () {
  30. app.use(stylus.middleware({ src: __dirname + '/public' }))
  31. app.use(express.static(__dirname + '/public'));
  32. app.use('/test', express.static(__dirname + '/../../test'));
  33. app.set('views', __dirname);
  34. app.set('view engine', 'jade');
  35. });
  36. /**
  37. * App routes.
  38. */
  39. app.get('/', function (req, res) {
  40. res.render('index', {
  41. layout: false
  42. , testsPorts: testsPorts
  43. , transport: transport
  44. });
  45. });
  46. /**
  47. * App listen.
  48. */
  49. app.listen(port++, function () {
  50. var addr = app.address();
  51. console.error(' listening on http://' + addr.address + ':' + addr.port);
  52. });
  53. /**
  54. * Override handler to simplify development
  55. */
  56. function handler (req, res) {
  57. fs.readFile(__dirname + '/../../dist/socket.io.js', 'utf8', function (err, b) {
  58. if (err) {
  59. res.writeHead(404);
  60. res.end('Error');
  61. return;
  62. }
  63. res.writeHead(200, { 'Content-Type': 'application/javascript' });
  64. res.end(b);
  65. });
  66. };
  67. /**
  68. * Socket.IO default server (to serve client)
  69. */
  70. var io = sio.listen(app);
  71. io.configure(function () {
  72. io.set('browser client handler', handler);
  73. io.set('transports', [
  74. transport
  75. ]);
  76. });
  77. /**
  78. * Scopes servers for a given test suite.
  79. */
  80. var currentSuite;
  81. function suite (name, fn) {
  82. currentSuite = testsPorts[name] = {};
  83. fn();
  84. };
  85. /**
  86. * Creates a socket io server
  87. */
  88. function server (name, fn) {
  89. currentSuite[name] = port;
  90. var io = sio.listen(port);
  91. io.configure(function () {
  92. io.set('transports', [transport]);
  93. });
  94. fn(io);
  95. port++;
  96. };
  97. /**
  98. * Socket.IO servers.
  99. */
  100. suite('socket.test.js', function () {
  101. server('test connecting the socket and disconnecting', function (io) {
  102. io.sockets.on('connection', function () {});
  103. });
  104. server('test receiving messages', function (io) {
  105. io.sockets.on('connection', function (socket) {
  106. var messages = 0;
  107. var interval = setInterval(function () {
  108. socket.send(++messages);
  109. if (messages == 3) {
  110. clearInterval(interval);
  111. setTimeout(function () {
  112. socket.disconnect();
  113. }, 500);
  114. }
  115. }, 50);
  116. });
  117. });
  118. server('test sending messages', function (io) {
  119. io.sockets.on('connection', function (socket) {
  120. socket.on('message', function (msg) {
  121. socket.send(msg);
  122. });
  123. });
  124. });
  125. server('test manual buffer flushing', function (io) {
  126. io.sockets.on('connection', function (socket) {
  127. socket.on('message', function (msg) {
  128. socket.send(msg);
  129. });
  130. });
  131. });
  132. server('test automatic buffer flushing', function (io) {
  133. io.sockets.on('connection', function (socket) {
  134. socket.on('message', function (msg) {
  135. socket.send(msg);
  136. });
  137. });
  138. });
  139. server('test acks sent from client', function (io) {
  140. io.sockets.on('connection', function (socket) {
  141. socket.send('tobi', function () {
  142. socket.send('tobi 2');
  143. });
  144. });
  145. });
  146. server('test acks sent from server', function (io) {
  147. io.sockets.on('connection', function (socket) {});
  148. });
  149. server('test connecting to namespaces', function (io) {
  150. io.of('/woot').on('connection', function (socket) {
  151. socket.send('connected to woot');
  152. });
  153. io.of('/chat').on('connection', function (socket) {
  154. socket.send('connected to chat');
  155. });
  156. });
  157. server('test disconnecting from namespaces', function (io) {
  158. io.of('/a').on('connection', function (socket) {});
  159. io.of('/b').on('connection', function (socket) {});
  160. });
  161. server('test authorizing for namespaces', function (io) {
  162. io.of('/a')
  163. .authorization(function (data, fn) {
  164. fn(null, false);
  165. })
  166. .on('connection', function (socket) {});
  167. });
  168. server('test sending json from server', function (io) {
  169. io.sockets.on('connection', function (socket) {
  170. io.sockets.json.send(3141592);
  171. });
  172. });
  173. server('test sending json from client', function (io) {
  174. io.sockets.on('connection', function (socket) {
  175. socket.on('message', function (arr) {
  176. if (Array.isArray(arr) && arr.length == 3) {
  177. socket.send('echo');
  178. }
  179. });
  180. });
  181. });
  182. server('test emitting an event from server', function (io) {
  183. io.sockets.on('connection', function (socket) {
  184. socket.emit('woot');
  185. });
  186. });
  187. server('test emitting multiple events at once to the server', function (io) {
  188. io.sockets.on('connection', function (socket) {
  189. var messages = [];
  190. socket.on('print', function (msg) {
  191. if (messages.indexOf(msg) >= 0) {
  192. console.error('duplicate message');
  193. }
  194. messages.push(msg);
  195. if (messages.length == 2) {
  196. socket.emit('done');
  197. }
  198. });
  199. });
  200. });
  201. server('test emitting an event to server', function (io) {
  202. io.sockets.on('connection', function (socket) {
  203. socket.on('woot', function () {
  204. socket.emit('echo');
  205. });
  206. });
  207. });
  208. server('test emitting an event from server and sending back data', function (io) {
  209. io.sockets.on('connection', function (socket) {
  210. socket.emit('woot', 1, function (a) {
  211. if (a === 'test') {
  212. socket.emit('done');
  213. }
  214. });
  215. });
  216. });
  217. server('test emitting an event to server and sending back data', function (io) {
  218. io.sockets.on('connection', function (socket) {
  219. socket.on('tobi', function (a, b, fn) {
  220. if (a === 1 && b === 2) {
  221. fn({ hello: 'world' });
  222. }
  223. });
  224. });
  225. });
  226. server('test encoding a payload', function (io) {
  227. io.of('/woot').on('connection', function (socket) {
  228. var count = 0;
  229. socket.on('message', function (a) {
  230. if (a == 'ñ') {
  231. if (++count == 4) {
  232. socket.emit('done');
  233. }
  234. }
  235. });
  236. });
  237. });
  238. server('test sending query strings to the server', function (io) {
  239. io.sockets.on('connection', function (socket) {
  240. socket.json.send(socket.handshake);
  241. })
  242. });
  243. server('test sending newline', function (io) {
  244. io.sockets.on('connection', function (socket) {
  245. socket.on('message', function (msg) {
  246. if (msg == '\n') {
  247. socket.emit('done');
  248. }
  249. });
  250. });
  251. });
  252. server('test sending unicode', function (io) {
  253. io.sockets.on('connection', function (socket) {
  254. socket.on('message', function (msg) {
  255. if (msg.test == "☃") {
  256. socket.emit('done');
  257. }
  258. });
  259. });
  260. });
  261. server('test webworker connection', function (io) {
  262. io.sockets.on('connection', function (socket) {
  263. socket.on('message', function (msg) {
  264. if (msg == 'woot') {
  265. socket.emit('done');
  266. }
  267. });
  268. });
  269. });
  270. });