PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/now/node_modules/socket.io/test/transports.websocket.test.js

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