PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/test/transports.websocket.test.js

https://github.com/lahorichargha/socket.io
JavaScript | 1894 lines | 1619 code | 260 blank | 15 comment | 157 complexity | 0b287f2cfa6ed2b59458da13a8a86c5e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Test dependencies.
  8. */
  9. var sio = require('socket.io')
  10. , should = require('./common')
  11. , parser = sio.parser
  12. , ports = 15800;
  13. /**
  14. * Tests.
  15. */
  16. module.exports = {
  17. 'websocket identifies as websocket': function (done) {
  18. var cl = client(++ports)
  19. , io = create(cl)
  20. , ws;
  21. io.set('transports', ['websocket']);
  22. io.sockets.on('connection', function (socket) {
  23. socket.manager.transports[socket.id].name.should.equal('websocket');
  24. ws.finishClose();
  25. cl.end();
  26. io.server.close();
  27. done();
  28. });
  29. cl.handshake(function (sid) {
  30. ws = websocket(cl, sid);
  31. });
  32. },
  33. 'default websocket draft parser is used for unknown sec-websocket-version': function (done) {
  34. var cl = client(++ports)
  35. , io = create(cl)
  36. , ws;
  37. io.set('transports', ['websocket']);
  38. io.sockets.on('connection', function (socket) {
  39. socket.manager.transports[socket.id].protocolVersion.should.equal('hixie-76');
  40. ws.finishClose();
  41. cl.end();
  42. io.server.close();
  43. done();
  44. });
  45. cl.handshake(function (sid) {
  46. ws = websocket(cl, sid);
  47. });
  48. },
  49. 'hybi-07-12 websocket draft parser is used for sec-websocket-version: 8': function (done) {
  50. var cl = client(++ports)
  51. , io = create(cl);
  52. io.set('transports', ['websocket']);
  53. io.sockets.on('connection', function (socket) {
  54. socket.manager.transports[socket.id].protocolVersion.should.equal('07-12');
  55. cl.end();
  56. io.server.close();
  57. done();
  58. });
  59. var headers = {
  60. 'sec-websocket-version': 8,
  61. 'upgrade': 'websocket',
  62. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  63. }
  64. cl.get('/socket.io/{protocol}', {}, function (res, data) {
  65. var sid = data.split(':')[0];
  66. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  67. cl.get(url, {headers: headers}, function (res, data) {});
  68. });
  69. },
  70. 'hybi-16 websocket draft parser is used for sec-websocket-version: 13': function (done) {
  71. var cl = client(++ports)
  72. , io = create(cl)
  73. io.set('transports', ['websocket']);
  74. io.sockets.on('connection', function (socket) {
  75. socket.manager.transports[socket.id].protocolVersion.should.equal('16');
  76. cl.end();
  77. io.server.close();
  78. done();
  79. });
  80. var headers = {
  81. 'sec-websocket-version': 13,
  82. 'upgrade': 'websocket',
  83. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  84. }
  85. cl.get('/socket.io/{protocol}', {}, function (res, data) {
  86. var sid = data.split(':')[0];
  87. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  88. cl.get(url, {headers: headers}, function (res, data) {});
  89. });
  90. },
  91. 'hybi-07-12 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
  92. var cl = client(++ports)
  93. , io = create(cl)
  94. io.set('transports', ['websocket']);
  95. io.set('origins', 'foo.bar.com:*');
  96. var notConnected = true;
  97. io.sockets.on('connection', function() {
  98. notConnected = false;
  99. });
  100. var headers = {
  101. 'sec-websocket-version': 8,
  102. 'upgrade': 'websocket',
  103. 'Sec-WebSocket-Origin': 'http://baz.bar.com',
  104. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  105. }
  106. // handshake uses correct origin -- we want to block the actual websocket call
  107. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  108. var sid = data.split(':')[0];
  109. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  110. var req = cl.get(url, {headers: headers}, function (res, data) {});
  111. var closed = false;
  112. req.on('close', function() {
  113. if (closed) return;
  114. closed = true;
  115. notConnected.should.be.true;
  116. cl.end();
  117. try {
  118. io.server.close();
  119. }
  120. catch (e) {}
  121. done();
  122. });
  123. });
  124. },
  125. 'hybi-16 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
  126. var cl = client(++ports)
  127. , io = create(cl);
  128. io.set('transports', ['websocket']);
  129. io.set('origins', 'foo.bar.com:*');
  130. var notConnected = true;
  131. io.sockets.on('connection', function() {
  132. notConnected = false;
  133. });
  134. var headers = {
  135. 'sec-websocket-version': 13,
  136. 'upgrade': 'websocket',
  137. 'origin': 'http://baz.bar.com',
  138. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  139. }
  140. // handshake uses correct origin -- we want to block the actual websocket call
  141. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  142. var sid = data.split(':')[0];
  143. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  144. var req = cl.get(url, {headers: headers}, function (res, data) {});
  145. var closed = false;
  146. req.on('close', function() {
  147. if (closed) return;
  148. closed = true;
  149. notConnected.should.be.true;
  150. cl.end();
  151. try {
  152. io.server.close();
  153. }
  154. catch (e) {}
  155. done();
  156. });
  157. });
  158. },
  159. 'hybi-07-12 origin filter accepts implicit port 80 for sec-websocket-origin': function (done) {
  160. done();return;
  161. var cl = client(++ports)
  162. , io = create(cl)
  163. io.set('transports', ['websocket']);
  164. io.set('origins', 'foo.bar.com:80');
  165. var headers = {
  166. 'sec-websocket-version': 8,
  167. 'upgrade': 'websocket',
  168. 'Sec-WebSocket-Origin': 'http://foo.bar.com',
  169. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  170. }
  171. io.sockets.on('connection', function() {
  172. cl.end();
  173. io.server.close();
  174. done();
  175. });
  176. // handshake uses correct origin -- we want to block the actual websocket call
  177. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  178. var sid = data.split(':')[0];
  179. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  180. cl.get(url, {headers: headers}, function (res, data) {});
  181. });
  182. },
  183. 'hybi-16 origin filter accepts implicit port 80 for sec-websocket-origin': function (done) {
  184. var cl = client(++ports)
  185. , io = create(cl)
  186. io.set('transports', ['websocket']);
  187. io.set('origins', 'foo.bar.com:80');
  188. var headers = {
  189. 'sec-websocket-version': 13,
  190. 'upgrade': 'websocket',
  191. 'origin': 'http://foo.bar.com',
  192. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  193. }
  194. io.sockets.on('connection', function() {
  195. cl.end();
  196. io.server.close();
  197. done();
  198. });
  199. // handshake uses correct origin -- we want to block the actual websocket call
  200. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  201. var sid = data.split(':')[0];
  202. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  203. cl.get(url, {headers: headers}, function (res, data) {});
  204. });
  205. },
  206. 'test that not responding to a heartbeat drops client': function (done) {
  207. var cl = client(++ports)
  208. , io = create(cl)
  209. , messages = 0
  210. , ws;
  211. io.configure(function () {
  212. io.set('heartbeat interval', .05);
  213. io.set('heartbeat timeout', .05);
  214. io.set('close timeout', 0);
  215. });
  216. io.sockets.on('connection', function (socket) {
  217. socket.on('disconnect', function (reason) {
  218. beat.should.be.true;
  219. reason.should.eql('heartbeat timeout');
  220. cl.end();
  221. ws.finishClose();
  222. io.server.close();
  223. done();
  224. });
  225. });
  226. cl.handshake(function (sid) {
  227. ws = websocket(cl, sid);
  228. ws.on('message', function (packet) {
  229. if (++messages == 1) {
  230. packet.type.should.eql('connect');
  231. } else {
  232. packet.type.should.eql('heartbeat');
  233. beat = true;
  234. }
  235. });
  236. });
  237. },
  238. 'test that responding to a heartbeat maintains session': function (done) {
  239. var cl = client(++ports)
  240. , io = create(cl)
  241. , messages = 0
  242. , heartbeats = 0
  243. , ws;
  244. io.configure(function () {
  245. io.set('heartbeat interval', .05);
  246. io.set('heartbeat timeout', .05);
  247. io.set('close timeout', 0);
  248. });
  249. io.sockets.on('connection', function (socket) {
  250. socket.on('disconnect', function (reason) {
  251. heartbeats.should.eql(2);
  252. reason.should.eql('heartbeat timeout');
  253. cl.end();
  254. ws.finishClose();
  255. io.server.close();
  256. done();
  257. });
  258. });
  259. cl.handshake(function (sid) {
  260. ws = websocket(cl, sid);
  261. ws.on('message', function (packet) {
  262. if (++messages == 1) {
  263. packet.type.should.eql('connect');
  264. } else {
  265. packet.type.should.eql('heartbeat');
  266. heartbeats++;
  267. if (heartbeats == 1) {
  268. ws.packet({ type: 'heartbeat' });
  269. }
  270. }
  271. });
  272. });
  273. },
  274. 'test sending deliverable volatile messages': function (done) {
  275. var cl = client(++ports)
  276. , io = create(cl)
  277. , messages = 0
  278. , messaged = false;
  279. io.configure(function () {
  280. io.set('close timeout', .05);
  281. });
  282. io.sockets.on('connection', function (socket) {
  283. socket.volatile.send('tobi');
  284. socket.on('disconnect', function () {
  285. messaged.should.be.true;
  286. cl.end();
  287. io.server.close();
  288. done();
  289. });
  290. });
  291. cl.handshake(function (sid) {
  292. var ws = websocket(cl, sid);
  293. ws.on('message', function (msg) {
  294. if (++messages == 1) {
  295. msg.type.should.eql('connect');
  296. } else {
  297. msg.should.eql({
  298. type: 'message'
  299. , data: 'tobi'
  300. , endpoint: ''
  301. });
  302. messaged = true;
  303. ws.finishClose();
  304. }
  305. });
  306. });
  307. },
  308. 'test sending deliverable volatile json': function (done) {
  309. var cl = client(++ports)
  310. , io = create(cl)
  311. , messaged = false;
  312. io.configure(function () {
  313. io.set('close timeout', .05);
  314. });
  315. io.sockets.on('connection', function (socket) {
  316. socket.volatile.json.send([1, 2, 3]);
  317. socket.on('disconnect', function () {
  318. messaged.should.be.true;
  319. cl.end();
  320. io.server.close();
  321. done();
  322. });
  323. });
  324. cl.handshake(function (sid) {
  325. var ws = websocket(cl, sid);
  326. ws.on('message', function (msg) {
  327. if (!ws.connected) {
  328. msg.type.should.eql('connect');
  329. ws.connected = true;
  330. } else {
  331. msg.should.eql({
  332. type: 'json'
  333. , data: [1, 2, 3]
  334. , endpoint: ''
  335. });
  336. messaged = true;
  337. ws.finishClose();
  338. }
  339. });
  340. });
  341. },
  342. 'test sending deliverable volatile events': function (done) {
  343. var cl = client(++ports)
  344. , io = create(cl)
  345. , messaged = false;
  346. io.configure(function () {
  347. io.set('close timeout', .05);
  348. });
  349. io.sockets.on('connection', function (socket) {
  350. socket.volatile.emit('tobi');
  351. socket.on('disconnect', function () {
  352. messaged.should.be.true;
  353. cl.end();
  354. io.server.close();
  355. done();
  356. });
  357. });
  358. cl.handshake(function (sid) {
  359. var ws = websocket(cl, sid);
  360. ws.on('message', function (msg) {
  361. if (!ws.connected) {
  362. msg.type.should.eql('connect');
  363. ws.connected = true;
  364. } else {
  365. msg.should.eql({
  366. type: 'event'
  367. , name: 'tobi'
  368. , endpoint: ''
  369. , args: []
  370. });
  371. messaged = true;
  372. ws.finishClose();
  373. }
  374. });
  375. });
  376. },
  377. 'test sending to all clients in a namespace': function (done) {
  378. var port = ++ports
  379. , cl1 = client(port)
  380. , cl2 = client(port)
  381. , io = create(cl1)
  382. , messages = 0
  383. , connections = 0
  384. , disconnections = 0;
  385. io.configure(function () {
  386. io.set('close timeout', 0);
  387. });
  388. io.sockets.on('connection', function (socket) {
  389. connections++;
  390. if (connections == 2) {
  391. io.sockets.send('yup');
  392. }
  393. socket.on('disconnect', function () {
  394. disconnections++;
  395. if (disconnections == 2) {
  396. messages.should.eql(2);
  397. cl1.end();
  398. cl2.end();
  399. io.server.close();
  400. done();
  401. }
  402. });
  403. });
  404. cl1.handshake(function (sid) {
  405. var ws1 = websocket(cl1, sid);
  406. ws1.on('message', function (msg) {
  407. if (!ws1.connected) {
  408. msg.type.should.eql('connect');
  409. ws1.connected = true;
  410. } else {
  411. msg.should.eql({
  412. type: 'message'
  413. , data: 'yup'
  414. , endpoint: ''
  415. });
  416. messages++;
  417. ws1.finishClose();
  418. }
  419. });
  420. });
  421. cl2.handshake(function (sid) {
  422. var ws2 = websocket(cl2, sid);
  423. ws2.on('message', function (msg) {
  424. if (!ws2.connected) {
  425. msg.type.should.eql('connect');
  426. ws2.connected = true;
  427. } else {
  428. msg.should.eql({
  429. type: 'message'
  430. , data: 'yup'
  431. , endpoint: ''
  432. });
  433. messages++;
  434. ws2.finishClose();
  435. }
  436. });
  437. });
  438. },
  439. 'test sending json to all clients in a namespace': function (done) {
  440. var port = ++ports
  441. , cl1 = client(port)
  442. , cl2 = client(port)
  443. , io = create(cl1)
  444. , messages = 0
  445. , connections = 0
  446. , disconnections = 0;
  447. io.configure(function () {
  448. io.set('close timeout', 0);
  449. });
  450. io.sockets.on('connection', function (socket) {
  451. connections++;
  452. if (connections == 2) {
  453. io.sockets.json.send({ a: 'b' });
  454. }
  455. socket.on('disconnect', function () {
  456. disconnections++;
  457. if (disconnections == 2) {
  458. messages.should.eql(2);
  459. cl1.end();
  460. cl2.end();
  461. io.server.close();
  462. done();
  463. }
  464. });
  465. });
  466. cl1.handshake(function (sid) {
  467. var ws1 = websocket(cl1, sid);
  468. ws1.on('message', function (msg) {
  469. if (!ws1.connected) {
  470. msg.type.should.eql('connect');
  471. ws1.connected = true;
  472. } else {
  473. msg.should.eql({
  474. type: 'json'
  475. , data: { a: 'b' }
  476. , endpoint: ''
  477. });
  478. messages++;
  479. ws1.finishClose();
  480. }
  481. });
  482. });
  483. cl2.handshake(function (sid) {
  484. var ws2 = websocket(cl2, sid);
  485. ws2.on('message', function (msg) {
  486. if (!ws2.connected) {
  487. msg.type.should.eql('connect');
  488. ws2.connected = true;
  489. } else {
  490. msg.should.eql({
  491. type: 'json'
  492. , data: { a: 'b' }
  493. , endpoint: ''
  494. });
  495. messages++;
  496. ws2.finishClose();
  497. }
  498. });
  499. });
  500. },
  501. 'test emitting to all clients in a namespace': function (done) {
  502. var port = ++ports
  503. , cl1 = client(port)
  504. , cl2 = client(port)
  505. , io = create(cl1)
  506. , messages = 0
  507. , connections = 0
  508. , disconnections = 0;
  509. io.configure(function () {
  510. io.set('close timeout', 0);
  511. });
  512. io.sockets.on('connection', function (socket) {
  513. connections++;
  514. if (connections == 2) {
  515. io.sockets.emit('tobi', 'rapture');
  516. }
  517. socket.on('disconnect', function () {
  518. disconnections++;
  519. if (disconnections == 2) {
  520. messages.should.eql(2);
  521. cl1.end();
  522. cl2.end();
  523. io.server.close();
  524. done();
  525. }
  526. });
  527. });
  528. cl1.handshake(function (sid) {
  529. var ws1 = websocket(cl1, sid);
  530. ws1.on('message', function (msg) {
  531. if (!ws1.connected) {
  532. msg.type.should.eql('connect');
  533. ws1.connected = true;
  534. } else {
  535. msg.should.eql({
  536. type: 'event'
  537. , name: 'tobi'
  538. , args: ['rapture']
  539. , endpoint: ''
  540. });
  541. messages++;
  542. ws1.finishClose();
  543. }
  544. });
  545. });
  546. cl2.handshake(function (sid) {
  547. var ws2 = websocket(cl2, sid);
  548. ws2.on('message', function (msg) {
  549. if (!ws2.connected) {
  550. msg.type.should.eql('connect');
  551. ws2.connected = true;
  552. } else {
  553. msg.should.eql({
  554. type: 'event'
  555. , name: 'tobi'
  556. , args: ['rapture']
  557. , endpoint: ''
  558. });
  559. messages++;
  560. ws2.finishClose();
  561. }
  562. });
  563. });
  564. },
  565. 'test sending to all clients in a room': function (done) {
  566. var port = ++ports
  567. , cl1 = client(port)
  568. , cl2 = client(port)
  569. , cl3 = client(port)
  570. , io = create(cl1)
  571. , messages = 0
  572. , joins = 0
  573. , connections = 0
  574. , disconnections = 0;
  575. io.configure(function () {
  576. io.set('close timeout', 0);
  577. });
  578. io.sockets.on('connection', function (socket) {
  579. connections++;
  580. if (connections != 3) {
  581. socket.join('woot');
  582. joins++;
  583. if (joins == 2) {
  584. setTimeout(function () {
  585. connections.should.eql(3);
  586. io.sockets.in('woot').send('hahaha');
  587. }, 20);
  588. }
  589. }
  590. socket.on('disconnect', function () {
  591. disconnections++;
  592. if (disconnections == 3) {
  593. messages.should.eql(2);
  594. cl1.end();
  595. cl2.end();
  596. cl3.end();
  597. io.server.close();
  598. done();
  599. }
  600. });
  601. });
  602. cl1.handshake(function (sid) {
  603. var ws1 = websocket(cl1, sid);
  604. ws1.on('message', function (msg) {
  605. if (!ws1.connected) {
  606. msg.type.should.eql('connect');
  607. ws1.connected = true;
  608. } else {
  609. msg.should.eql({
  610. type: 'message'
  611. , data: 'hahaha'
  612. , endpoint: ''
  613. });
  614. messages++;
  615. }
  616. });
  617. setTimeout(function () {
  618. ws1.finishClose();
  619. }, 50);
  620. });
  621. cl2.handshake(function (sid) {
  622. var ws2 = websocket(cl2, sid);
  623. ws2.on('message', function (msg) {
  624. if (!ws2.connected) {
  625. msg.type.should.eql('connect');
  626. ws2.connected = true;
  627. } else {
  628. msg.should.eql({
  629. type: 'message'
  630. , data: 'hahaha'
  631. , endpoint: ''
  632. });
  633. messages++;
  634. }
  635. });
  636. setTimeout(function () {
  637. ws2.finishClose();
  638. }, 50);
  639. });
  640. cl3.handshake(function (sid) {
  641. var ws3 = websocket(cl3, sid);
  642. ws3.on('message', function (msg) {
  643. if (!ws3.connected) {
  644. msg.type.should.eql('connect');
  645. ws3.connected = true;
  646. } else {
  647. msg.should.eql({
  648. type: 'message'
  649. , data: 'hahaha'
  650. , endpoint: ''
  651. });
  652. messages++;
  653. }
  654. });
  655. setTimeout(function () {
  656. ws3.finishClose();
  657. }, 50);
  658. });
  659. },
  660. 'test sending json to all clients in a room': function (done) {
  661. var port = ++ports
  662. , cl1 = client(port)
  663. , cl2 = client(port)
  664. , cl3 = client(port)
  665. , io = create(cl1)
  666. , messages = 0
  667. , joins = 0
  668. , connections = 0
  669. , disconnections = 0;
  670. io.configure(function () {
  671. io.set('close timeout', 0);
  672. });
  673. io.sockets.on('connection', function (socket) {
  674. connections++;
  675. if (connections != 3) {
  676. socket.join('woot');
  677. joins++;
  678. if (joins == 2) {
  679. setTimeout(function () {
  680. connections.should.eql(3);
  681. io.sockets.in('woot').json.send(123);
  682. }, 20);
  683. }
  684. }
  685. socket.on('disconnect', function () {
  686. disconnections++;
  687. if (disconnections == 3) {
  688. messages.should.eql(2);
  689. cl1.end();
  690. cl2.end();
  691. cl3.end();
  692. io.server.close();
  693. done();
  694. }
  695. });
  696. });
  697. cl1.handshake(function (sid) {
  698. var ws1 = websocket(cl1, sid);
  699. ws1.on('message', function (msg) {
  700. if (!ws1.connected) {
  701. msg.type.should.eql('connect');
  702. ws1.connected = true;
  703. } else {
  704. msg.should.eql({
  705. type: 'json'
  706. , data: 123
  707. , endpoint: ''
  708. });
  709. messages++;
  710. }
  711. });
  712. setTimeout(function () {
  713. ws1.finishClose();
  714. }, 50);
  715. });
  716. cl2.handshake(function (sid) {
  717. var ws2 = websocket(cl2, sid);
  718. ws2.on('message', function (msg) {
  719. if (!ws2.connected) {
  720. msg.type.should.eql('connect');
  721. ws2.connected = true;
  722. } else {
  723. msg.should.eql({
  724. type: 'json'
  725. , data: 123
  726. , endpoint: ''
  727. });
  728. messages++;
  729. }
  730. });
  731. setTimeout(function () {
  732. ws2.finishClose();
  733. }, 50);
  734. });
  735. cl3.handshake(function (sid) {
  736. var ws3 = websocket(cl3, sid);
  737. ws3.on('message', function (msg) {
  738. if (!ws3.connected) {
  739. msg.type.should.eql('connect');
  740. ws3.connected = true;
  741. } else {
  742. msg.should.eql({
  743. type: 'json'
  744. , data: 123
  745. , endpoint: ''
  746. });
  747. messages++;
  748. }
  749. });
  750. setTimeout(function () {
  751. ws3.finishClose();
  752. }, 50);
  753. });
  754. },
  755. 'test emitting to all clients in a room': function (done) {
  756. var port = ++ports
  757. , cl1 = client(port)
  758. , cl2 = client(port)
  759. , cl3 = client(port)
  760. , io = create(cl1)
  761. , messages = 0
  762. , joins = 0
  763. , connections = 0
  764. , disconnections = 0;
  765. io.configure(function () {
  766. io.set('close timeout', 0);
  767. });
  768. io.sockets.on('connection', function (socket) {
  769. connections++;
  770. if (connections != 3) {
  771. socket.join('woot');
  772. joins++;
  773. if (joins == 2) {
  774. setTimeout(function () {
  775. connections.should.eql(3);
  776. io.sockets.in('woot').emit('locki');
  777. }, 20);
  778. }
  779. }
  780. socket.on('disconnect', function () {
  781. disconnections++;
  782. if (disconnections == 3) {
  783. messages.should.eql(2);
  784. cl1.end();
  785. cl2.end();
  786. cl3.end();
  787. io.server.close();
  788. done();
  789. }
  790. });
  791. });
  792. cl1.handshake(function (sid) {
  793. var ws1 = websocket(cl1, sid);
  794. ws1.on('message', function (msg) {
  795. if (!ws1.connected) {
  796. msg.type.should.eql('connect');
  797. ws1.connected = true;
  798. } else {
  799. msg.should.eql({
  800. type: 'event'
  801. , name: 'locki'
  802. , args: []
  803. , endpoint: ''
  804. });
  805. messages++;
  806. }
  807. });
  808. setTimeout(function () {
  809. ws1.finishClose();
  810. }, 50);
  811. });
  812. cl2.handshake(function (sid) {
  813. var ws2 = websocket(cl2, sid);
  814. ws2.on('message', function (msg) {
  815. if (!ws2.connected) {
  816. msg.type.should.eql('connect');
  817. ws2.connected = true;
  818. } else {
  819. msg.should.eql({
  820. type: 'event'
  821. , name: 'locki'
  822. , args: []
  823. , endpoint: ''
  824. });
  825. messages++;
  826. }
  827. });
  828. setTimeout(function () {
  829. ws2.finishClose();
  830. }, 50);
  831. });
  832. cl3.handshake(function (sid) {
  833. var ws3 = websocket(cl3, sid);
  834. ws3.on('message', function (msg) {
  835. if (!ws3.connected) {
  836. msg.type.should.eql('connect');
  837. ws3.connected = true;
  838. } else {
  839. msg.should.eql({
  840. type: 'event'
  841. , name: 'locki'
  842. , args: []
  843. , endpoint: ''
  844. });
  845. messages++;
  846. }
  847. });
  848. setTimeout(function () {
  849. ws3.finishClose();
  850. }, 50);
  851. });
  852. },
  853. 'test leaving a room': function (done) {
  854. var port = ++ports
  855. , cl1 = client(port)
  856. , cl2 = client(port)
  857. , io = create(cl1)
  858. , joins = 0
  859. , disconnects = 0;
  860. io.set('close timeout', 0);
  861. io.sockets.on('connection', function (socket) {
  862. socket.join('foo');
  863. io.sockets.clients('foo').should.have.length(++joins);
  864. socket.on('disconnect', function () {
  865. socket.leave('foo');
  866. socket.leave('foo');
  867. socket.leave('foo');
  868. io.sockets.clients('foo').should.have.length(--joins);
  869. if (++disconnects == 2) {
  870. io.server.close();
  871. cl1.end();
  872. cl2.end();
  873. done();
  874. }
  875. })
  876. });
  877. cl1.handshake(function (sid) {
  878. var ws1 = websocket(cl1, sid);
  879. ws1.on('message', function (msg) {
  880. if (!ws1.connected) {
  881. msg.type.should.eql('connect');
  882. ws1.connected = true;
  883. ws1.finishClose();
  884. }
  885. });
  886. });
  887. cl2.handshake(function (sid) {
  888. var ws2 = websocket(cl2, sid);
  889. ws2.on('message', function (msg) {
  890. if (!ws2.connected) {
  891. msg.type.should.eql('connect');
  892. ws2.connected = true;
  893. ws2.finishClose();
  894. }
  895. });
  896. });
  897. },
  898. 'test message with broadcast flag': function (done) {
  899. var port = ++ports
  900. , cl1 = client(port)
  901. , cl2 = client(port)
  902. , cl3 = client(port)
  903. , io = create(cl1)
  904. , messages = 0
  905. , disconnections = 0;
  906. io.configure(function () {
  907. io.set('close timeout', 0);
  908. });
  909. io.sockets.on('connection', function (socket) {
  910. socket.on('trigger broadcast', function () {
  911. socket.broadcast.send('boom');
  912. });
  913. socket.on('disconnect', function () {
  914. disconnections++;
  915. if (disconnections == 3) {
  916. messages.should.eql(2);
  917. cl1.end();
  918. cl2.end();
  919. cl3.end();
  920. io.server.close();
  921. done();
  922. }
  923. });
  924. });
  925. cl1.handshake(function (sid) {
  926. var ws1 = websocket(cl1, sid);
  927. ws1.on('message', function (msg) {
  928. if (!ws1.connected) {
  929. msg.type.should.eql('connect');
  930. ws1.connected = true;
  931. } else {
  932. msg.should.eql({
  933. type: 'message'
  934. , data: 'boom'
  935. , endpoint: ''
  936. });
  937. messages++;
  938. ws1.finishClose();
  939. }
  940. });
  941. });
  942. cl2.handshake(function (sid) {
  943. var ws2 = websocket(cl2, sid);
  944. ws2.on('message', function (msg) {
  945. if (!ws2.connected) {
  946. msg.type.should.eql('connect');
  947. ws2.connected = true;
  948. } else {
  949. msg.should.eql({
  950. type: 'message'
  951. , data: 'boom'
  952. , endpoint: ''
  953. });
  954. messages++;
  955. ws2.finishClose();
  956. }
  957. });
  958. });
  959. cl3.handshake(function (sid) {
  960. var ws3 = websocket(cl3, sid);
  961. ws3.on('open', function () {
  962. ws3.packet({
  963. type: 'event'
  964. , name: 'trigger broadcast'
  965. , endpoint: ''
  966. });
  967. setTimeout(function () {
  968. ws3.finishClose();
  969. }, 50);
  970. });
  971. ws3.on('message', function (msg) {
  972. if (!ws3.connected) {
  973. msg.type.should.eql('connect');
  974. ws3.connected = true;
  975. } else {
  976. throw new Error('we shouldnt get a message here');
  977. }
  978. });
  979. });
  980. },
  981. 'test json with broadcast flag': function (done) {
  982. var port = ++ports
  983. , cl1 = client(port)
  984. , cl2 = client(port)
  985. , cl3 = client(port)
  986. , io = create(cl1)
  987. , messages = 0
  988. , disconnections = 0;
  989. io.configure(function () {
  990. io.set('close timeout', 0);
  991. });
  992. io.sockets.on('connection', function (socket) {
  993. socket.on('trigger broadcast', function () {
  994. socket.broadcast.json.send([1, 2, 3]);
  995. });
  996. socket.on('disconnect', function () {
  997. disconnections++;
  998. if (disconnections == 3) {
  999. messages.should.eql(2);
  1000. cl1.end();
  1001. cl2.end();
  1002. cl3.end();
  1003. io.server.close();
  1004. done();
  1005. }
  1006. });
  1007. });
  1008. cl1.handshake(function (sid) {
  1009. var ws1 = websocket(cl1, sid);
  1010. ws1.on('message', function (msg) {
  1011. if (!ws1.connected) {
  1012. msg.type.should.eql('connect');
  1013. ws1.connected = true;
  1014. } else {
  1015. msg.should.eql({
  1016. type: 'json'
  1017. , data: [1, 2, 3]
  1018. , endpoint: ''
  1019. });
  1020. messages++;
  1021. ws1.finishClose();
  1022. }
  1023. });
  1024. });
  1025. cl2.handshake(function (sid) {
  1026. var ws2 = websocket(cl2, sid);
  1027. ws2.on('message', function (msg) {
  1028. if (!ws2.connected) {
  1029. msg.type.should.eql('connect');
  1030. ws2.connected = true;
  1031. } else {
  1032. msg.should.eql({
  1033. type: 'json'
  1034. , data: [1, 2, 3]
  1035. , endpoint: ''
  1036. });
  1037. messages++;
  1038. ws2.finishClose();
  1039. }
  1040. });
  1041. });
  1042. cl3.handshake(function (sid) {
  1043. var ws3 = websocket(cl3, sid);
  1044. ws3.on('open', function () {
  1045. ws3.packet({
  1046. type: 'event'
  1047. , name: 'trigger broadcast'
  1048. , endpoint: ''
  1049. });
  1050. setTimeout(function () {
  1051. ws3.finishClose();
  1052. }, 50);
  1053. });
  1054. ws3.on('message', function (msg) {
  1055. if (!ws3.connected) {
  1056. msg.type.should.eql('connect');
  1057. ws3.connected = true;
  1058. } else {
  1059. throw new Error('we shouldnt get a message here');
  1060. }
  1061. });
  1062. });
  1063. },
  1064. 'test event with broadcast flag': function (done) {
  1065. var port = ++ports
  1066. , cl1 = client(port)
  1067. , cl2 = client(port)
  1068. , cl3 = client(port)
  1069. , io = create(cl1)
  1070. , messages = 0
  1071. , disconnections = 0;
  1072. io.configure(function () {
  1073. io.set('close timeout', 0);
  1074. });
  1075. io.sockets.on('connection', function (socket) {
  1076. socket.on('trigger broadcast', function () {
  1077. socket.broadcast.emit('hey', 'arnold');
  1078. });
  1079. socket.on('disconnect', function () {
  1080. disconnections++;
  1081. if (disconnections == 3) {
  1082. messages.should.eql(2);
  1083. cl1.end();
  1084. cl2.end();
  1085. cl3.end();
  1086. io.server.close();
  1087. done();
  1088. }
  1089. });
  1090. });
  1091. cl1.handshake(function (sid) {
  1092. var ws1 = websocket(cl1, sid);
  1093. ws1.on('message', function (msg) {
  1094. if (!ws1.connected) {
  1095. msg.type.should.eql('connect');
  1096. ws1.connected = true;
  1097. } else {
  1098. msg.should.eql({
  1099. type: 'event'
  1100. , name: 'hey'
  1101. , args: ['arnold']
  1102. , endpoint: ''
  1103. });
  1104. messages++;
  1105. ws1.finishClose();
  1106. }
  1107. });
  1108. });
  1109. cl2.handshake(function (sid) {
  1110. var ws2 = websocket(cl2, sid);
  1111. ws2.on('message', function (msg) {
  1112. if (!ws2.connected) {
  1113. msg.type.should.eql('connect');
  1114. ws2.connected = true;
  1115. } else {
  1116. msg.should.eql({
  1117. type: 'event'
  1118. , name: 'hey'
  1119. , args: ['arnold']
  1120. , endpoint: ''
  1121. });
  1122. messages++;
  1123. ws2.finishClose();
  1124. }
  1125. });
  1126. });
  1127. cl3.handshake(function (sid) {
  1128. var ws3 = websocket(cl3, sid);
  1129. ws3.on('open', function () {
  1130. ws3.packet({
  1131. type: 'event'
  1132. , name: 'trigger broadcast'
  1133. , endpoint: ''
  1134. });
  1135. setTimeout(function () {
  1136. ws3.finishClose();
  1137. }, 50);
  1138. });
  1139. ws3.on('message', function (msg) {
  1140. if (!ws3.connected) {
  1141. msg.type.should.eql('connect');
  1142. ws3.connected = true;
  1143. } else {
  1144. throw new Error('we shouldnt get a message here');
  1145. }
  1146. });
  1147. });
  1148. },
  1149. 'test message with broadcast flag and to()': function (done) {
  1150. var port = ++ports
  1151. , cl1 = client(port)
  1152. , cl2 = client(port)
  1153. , cl3 = client(port)
  1154. , io = create(cl1)
  1155. , messages = 0
  1156. , connections = 0
  1157. , disconnections = 0;
  1158. io.configure(function () {
  1159. io.set('close timeout', 0);
  1160. });
  1161. io.sockets.on('connection', function (socket) {
  1162. connections++;
  1163. if (connections == 1) {
  1164. socket.join('losers');
  1165. }
  1166. socket.on('trigger broadcast', function () {
  1167. socket.broadcast.to('losers').send('boom');
  1168. });
  1169. socket.on('disconnect', function () {
  1170. disconnections++;
  1171. if (disconnections == 3) {
  1172. messages.should.eql(1);
  1173. cl1.end();
  1174. cl2.end();
  1175. cl3.end();
  1176. io.server.close();
  1177. done();
  1178. }
  1179. });
  1180. });
  1181. cl1.handshake(function (sid) {
  1182. var ws1 = websocket(cl1, sid);
  1183. ws1.on('message', function (msg) {
  1184. if (!ws1.connected) {
  1185. msg.type.should.eql('connect');
  1186. ws1.connected = true;
  1187. } else {
  1188. msg.should.eql({
  1189. type: 'message'
  1190. , data: 'boom'
  1191. , endpoint: ''
  1192. });
  1193. messages++;
  1194. }
  1195. });
  1196. ws1.on('open', function () {
  1197. cl2.handshake(function (sid) {
  1198. var ws2 = websocket(cl2, sid);
  1199. ws2.on('message', function (msg) {
  1200. if (!ws2.connected) {
  1201. msg.type.should.eql('connect');
  1202. ws2.connected = true;
  1203. } else {
  1204. throw new Error('This socket shouldnt get a message');
  1205. }
  1206. });
  1207. ws2.on('open', function () {
  1208. cl3.handshake(function (sid) {
  1209. var ws3 = websocket(cl3, sid);
  1210. ws3.on('open', function () {
  1211. ws3.packet({
  1212. type: 'event'
  1213. , name: 'trigger broadcast'
  1214. , endpoint: ''
  1215. });
  1216. setTimeout(function () {
  1217. ws1.finishClose();
  1218. ws2.finishClose();
  1219. ws3.finishClose();
  1220. }, 50);
  1221. });
  1222. ws3.on('message', function (msg) {
  1223. if (!ws3.connected) {
  1224. msg.type.should.eql('connect');
  1225. ws3.connected = true;
  1226. } else {
  1227. throw new Error('we shouldnt get a message here');
  1228. }
  1229. });
  1230. });
  1231. });
  1232. });
  1233. });
  1234. });
  1235. },
  1236. 'test json with broadcast flag and to()': function (done) {
  1237. var port = ++ports
  1238. , cl1 = client(port)
  1239. , cl2 = client(port)
  1240. , cl3 = client(port)
  1241. , io = create(cl1)
  1242. , messages = 0
  1243. , connections = 0
  1244. , disconnections = 0;
  1245. io.configure(function () {
  1246. io.set('close timeout', 0);
  1247. });
  1248. io.sockets.on('connection', function (socket) {
  1249. connections++;
  1250. if (connections == 1) {
  1251. socket.join('losers');
  1252. }
  1253. socket.on('trigger broadcast', function () {
  1254. socket.broadcast.json.to('losers').send({ hello: 'world' });
  1255. });
  1256. socket.on('disconnect', function () {
  1257. disconnections++;
  1258. if (disconnections == 3) {
  1259. messages.should.eql(1);
  1260. cl1.end();
  1261. cl2.end();
  1262. cl3.end();
  1263. io.server.close();
  1264. done();
  1265. }
  1266. });
  1267. });
  1268. cl1.handshake(function (sid) {
  1269. var ws1 = websocket(cl1, sid);
  1270. ws1.on('message', function (msg) {
  1271. if (!ws1.connected) {
  1272. msg.type.should.eql('connect');
  1273. ws1.connected = true;
  1274. } else {
  1275. msg.should.eql({
  1276. type: 'json'
  1277. , data: { hello: 'world' }
  1278. , endpoint: ''
  1279. });
  1280. messages++;
  1281. }
  1282. });
  1283. ws1.on('open', function () {
  1284. cl2.handshake(function (sid) {
  1285. var ws2 = websocket(cl2, sid);
  1286. ws2.on('message', function (msg) {
  1287. if (!ws2.connected) {
  1288. msg.type.should.eql('connect');
  1289. ws2.connected = true;
  1290. } else {
  1291. throw new Error('This socket shouldnt get a message');
  1292. }
  1293. });
  1294. ws2.on('open', function () {
  1295. cl3.handshake(function (sid) {
  1296. var ws3 = websocket(cl3, sid);
  1297. ws3.on('open', function () {
  1298. ws3.packet({
  1299. type: 'event'
  1300. , name: 'trigger broadcast'
  1301. , endpoint: ''
  1302. });
  1303. setTimeout(function () {
  1304. ws1.finishClose();
  1305. ws2.finishClose();
  1306. ws3.finishClose();
  1307. }, 50);
  1308. });
  1309. ws3.on('message', function (msg) {
  1310. if (!ws3.connected) {
  1311. msg.type.should.eql('connect');
  1312. ws3.connected = true;
  1313. } else {
  1314. throw new Error('we shouldnt get a message here');
  1315. }
  1316. });
  1317. });
  1318. });
  1319. });
  1320. });
  1321. });
  1322. },
  1323. 'test event with broadcast flag and to()': function (done) {
  1324. var port = ++ports
  1325. , cl1 = client(port)
  1326. , cl2 = client(port)
  1327. , cl3 = client(port)
  1328. , io = create(cl1)
  1329. , messages = 0
  1330. , connections = 0
  1331. , disconnections = 0;
  1332. io.configure(function () {
  1333. io.set('close timeout', 0);
  1334. });
  1335. io.sockets.on('connection', function (socket) {
  1336. connections++;
  1337. if (connections == 1) {
  1338. socket.join('losers');
  1339. }
  1340. socket.on('trigger broadcast', function () {
  1341. socket.broadcast.to('losers').emit('victory');
  1342. });
  1343. socket.on('disconnect', function () {
  1344. disconnections++;
  1345. if (disconnections == 3) {
  1346. messages.should.eql(1);
  1347. cl1.end();
  1348. cl2.end();
  1349. cl3.end();
  1350. io.server.close();
  1351. done();
  1352. }
  1353. });
  1354. });
  1355. cl1.handshake(function (sid) {
  1356. var ws1 = websocket(cl1, sid);
  1357. ws1.on('message', function (msg) {
  1358. if (!ws1.connected) {
  1359. msg.type.should.eql('connect');
  1360. ws1.connected = true;
  1361. } else {
  1362. msg.should.eql({
  1363. type: 'event'
  1364. , name: 'victory'
  1365. , args: []
  1366. , endpoint: ''
  1367. });
  1368. messages++;
  1369. }
  1370. });
  1371. ws1.on('open', function () {
  1372. cl2.handshake(function (sid) {
  1373. var ws2 = websocket(cl2, sid);
  1374. ws2.on('message', function (msg) {
  1375. if (!ws2.connected) {
  1376. msg.type.should.eql('connect');
  1377. ws2.connected = true;
  1378. } else {
  1379. throw new Error('This socket shouldnt get a message');
  1380. };
  1381. });
  1382. ws2.on('open', function () {
  1383. cl3.handshake(function (sid) {
  1384. var ws3 = websocket(cl3, sid);
  1385. ws3.on('open', function () {
  1386. ws3.packet({
  1387. type: 'event'
  1388. , name: 'trigger broadcast'
  1389. , endpoint: ''
  1390. });
  1391. setTimeout(function () {
  1392. ws1.finishClose();
  1393. ws2.finishClose();
  1394. ws3.finishClose();
  1395. }, 50);
  1396. });
  1397. ws3.on('message', function (msg) {
  1398. if (!ws3.connected) {
  1399. msg.type.should.eql('connect');
  1400. ws3.connected = true;
  1401. } else {
  1402. throw new Error('we shouldnt get a message here');
  1403. }
  1404. });
  1405. });
  1406. });
  1407. });
  1408. });
  1409. });
  1410. },
  1411. 'test accessing handshake data from sockets': function (done) {
  1412. var cl = client(++ports)
  1413. , io = create(cl)
  1414. , ws;
  1415. io.sockets.on('connection', function (socket) {
  1416. (!!socket.handshake.address.address).should.be.true;
  1417. (!!socket.handshake.address.port).should.be.true;
  1418. socket.handshake.headers.host.should.equal('localhost');
  1419. socket.handshake.headers.connection.should.equal('keep-alive');
  1420. socket.handshake.time.should.match(/GMT/);
  1421. socket.on('disconnect', function () {
  1422. setTimeout(function () {
  1423. ws.finishClose();
  1424. cl.end();
  1425. io.server.close();
  1426. done();
  1427. }, 10);
  1428. });
  1429. socket.disconnect();
  1430. });
  1431. cl.handshake(function (sid) {
  1432. ws = websocket(cl, sid);
  1433. ws.on('message', function (msg) {
  1434. if (!ws.connected) {
  1435. msg.type.should.eql('connect');
  1436. ws.connected = true;
  1437. }
  1438. });
  1439. });
  1440. },
  1441. 'test accessing the array of clients': function (done) {
  1442. var port = ++ports
  1443. , cl1 = client(port)
  1444. , cl2 = client(port)
  1445. , io = create(cl1)
  1446. , total = 2
  1447. , ws1, ws2;
  1448. io.sockets.on('connection', function (socket) {
  1449. socket.on('join ferrets', function () {
  1450. socket.join('ferrets');
  1451. socket.send('done');
  1452. });
  1453. });
  1454. function check() {
  1455. io.sockets.clients('ferrets').should.have.length(1);
  1456. io.sockets.clients('ferrets')[0].should.be.an.instanceof(sio.Socket);
  1457. io.sockets.clients('ferrets')[0].id.should.equal(ws1.sid);
  1458. io.sockets.clients().should.have.length(2);
  1459. io.sockets.clients()[0].should.be.an.instanceof(sio.Socket);
  1460. io.sockets.clients()[0].id.should.equal(ws1.sid);
  1461. io.sockets.clients()[1].should.be.an.instanceof(sio.Socket);
  1462. io.sockets.clients()[1].id.should.equal(ws2.sid);
  1463. ws1.finishClose();
  1464. ws2.finishClose();
  1465. cl1.end();
  1466. cl2.end();
  1467. io.server.close();
  1468. done();
  1469. };
  1470. cl1.handshake(function (sid) {
  1471. ws1 = websocket(cl1, sid);
  1472. ws1.sid = sid;
  1473. ws1.on('message', function (msg) {
  1474. if (!ws1.connected) {
  1475. msg.type.should.eql('connect');
  1476. ws1.connected = true;
  1477. ws1.packet({
  1478. type: 'event'
  1479. , name: 'join ferrets'
  1480. , endpoint: ''
  1481. });
  1482. } else {
  1483. cl2.handshake(function (sid) {
  1484. ws2 = websocket(cl2, sid);
  1485. ws2.sid = sid;
  1486. ws2.on('message', function (msg) {
  1487. if (!ws2.connected) {
  1488. msg.type.should.eql('connect');
  1489. ws2.connected = true;
  1490. check();
  1491. }
  1492. });
  1493. });
  1494. }
  1495. });
  1496. });
  1497. },
  1498. 'test accessing handshake data from sockets on disconnect': function (done) {
  1499. var cl = client(++ports)
  1500. , io = create(cl)
  1501. , ws;
  1502. io.sockets.on('connection', function (socket) {
  1503. socket.on('disconnect', function () {
  1504. (!!socket.handshake.address.address).should.be.true;
  1505. (!!socket.handshake.address.port).should.be.true;
  1506. socket.handshake.headers.host.should.equal('localhost');
  1507. socket.handshake.headers.connection.should.equal('keep-alive');
  1508. socket.handshake.time.should.match(/GMT/);
  1509. setTimeout(function () {
  1510. ws.finishClose();
  1511. cl.end();
  1512. io.server.close();
  1513. done();
  1514. }, 10);
  1515. });
  1516. socket.disconnect();
  1517. });
  1518. cl.handshake(function (sid) {
  1519. ws = websocket(cl, sid);
  1520. ws.on('message', function (msg) {
  1521. if (!ws.connected) {
  1522. msg.type.should.eql('connect');
  1523. ws.connected = true;
  1524. }
  1525. });
  1526. });
  1527. },
  1528. 'test for intentional and unintentional disconnects': function (done) {
  1529. var cl = client(++ports)
  1530. , io = create(cl)
  1531. , calls = 0
  1532. , ws;
  1533. function close () {
  1534. cl.end();
  1535. io.server.close();
  1536. ws.finishClose();
  1537. done();
  1538. }
  1539. io.configure(function () {
  1540. io.set('heartbeat interval', .05);
  1541. io.set('heartbeat timeout', .05);
  1542. io.set('close timeout', 0);
  1543. });
  1544. io.of('/foo').on('connection', function (socket) {
  1545. socket.on('disconnect', function (reason) {
  1546. reason.should.equal('packet');
  1547. if (++calls == 2) close();
  1548. });
  1549. });
  1550. io.of('/bar').on('connection', function (socket) {
  1551. socket.on('disconnect', function (reason) {
  1552. reason.should.equal('socket end');
  1553. if (++calls == 2) close();
  1554. });
  1555. });
  1556. cl.handshake(function (sid) {
  1557. var messages = 0;
  1558. ws = websocket(cl, sid);
  1559. ws.on('open', function () {
  1560. ws.packet({
  1561. type: 'connect'
  1562. , endpoint: '/foo'
  1563. });
  1564. ws.packet({
  1565. type: 'connect'
  1566. , endpoint: '/bar'
  1567. });
  1568. });
  1569. ws.on('message', function (packet) {
  1570. if (packet.type == 'connect') {
  1571. if (++messages === 3) {
  1572. ws.packet({ type: 'disconnect', endpoint:'/foo' });
  1573. ws.finishClose();
  1574. }
  1575. }
  1576. });
  1577. });
  1578. },
  1579. 'test socket clean up': function (done) {
  1580. var cl = client(++ports)
  1581. , io = create(cl)
  1582. , ws;
  1583. io.sockets.on('connection', function (socket) {
  1584. var self = this
  1585. , id = socket.id;
  1586. socket.on('disconnect', function () {
  1587. setTimeout(function () {
  1588. var available = !!self.sockets[id];
  1589. available.should.be.false;
  1590. ws.finishClose();
  1591. cl.end();
  1592. io.server.close();
  1593. done();
  1594. }, 10);
  1595. });
  1596. socket.disconnect();
  1597. });
  1598. cl.handshake(function (sid) {
  1599. ws = websocket(cl, sid);
  1600. ws.on('message', function (msg) {
  1601. if (!ws.connected) {
  1602. msg.type.should.eql('connect');
  1603. ws.connected = true;
  1604. }
  1605. });
  1606. });
  1607. },
  1608. 'accessing the transport type': function (done) {
  1609. var cl = client(++ports)
  1610. , io = create(cl)
  1611. , ws;
  1612. io.sockets.on('connection', function (socket) {
  1613. socket.transport.should.equal('websocket');
  1614. socket.on('disconnect', function () {
  1615. setTimeout(function () {
  1616. ws.finishClose();
  1617. cl.end();
  1618. io.server.close();
  1619. done();
  1620. }, 10);
  1621. });
  1622. socket.disconnect();
  1623. });
  1624. cl.handshake(function (sid) {
  1625. ws = websocket(cl, sid);
  1626. ws.on('message', function (msg) {
  1627. if (!ws.connected) {
  1628. msg.type.should.eql('connect');
  1629. ws.connected = true;
  1630. }
  1631. });
  1632. });
  1633. }
  1634. };