PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/node-websocket-client/test/test-basic.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 63 lines | 50 code | 11 blank | 2 comment | 0 complexity | 8591d37d73ea9e461d72d9634b4d6802 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. // Verify that we can connect to a WebSocket server, exchange messages, and
  2. // shut down cleanly.
  3. var assert = require('assert');
  4. var WebSocket = require('../lib/websocket').WebSocket;
  5. var WebSocketServer = require('websocket-server/ws').Server;
  6. var PORT = 1024 + Math.floor(Math.random() * 4096);
  7. var C_MSG = 'Client test: ' + (Math.random() * 100);
  8. var S_MSG = 'Server test: ' + (Math.random() * 100);
  9. var serverGotConnection = false;
  10. var clientGotOpen = false;
  11. var clientGotData = false;
  12. var clientGotMessage = false;
  13. var serverGotMessage = false;
  14. var serverGotClose = false;
  15. var wss = new WebSocketServer();
  16. wss.listen(PORT, 'localhost');
  17. wss.addListener('connection', function(c) {
  18. serverGotConnection = true;
  19. c.write(S_MSG);
  20. c.addListener('message', function(m) {
  21. assert.equal(m, C_MSG);
  22. serverGotMessage = true;
  23. });
  24. c.addListener('close', function() {
  25. serverGotClose = true;
  26. wss.close();
  27. });
  28. });
  29. var ws = new WebSocket('ws://localhost:' + PORT + '/', 'biff');
  30. ws.addListener('open', function() {
  31. clientGotOpen = true;
  32. });
  33. ws.addListener('data', function(buf) {
  34. assert.equal(typeof buf, 'object');
  35. assert.equal(buf.toString('utf8'), S_MSG);
  36. clientGotData = true;
  37. ws.send(C_MSG);
  38. ws.close();
  39. });
  40. ws.onmessage = function(m) {
  41. assert.equal(m, S_MSG);
  42. assert.equal(typeof m, 'string');
  43. clientGotMessage = true;
  44. };
  45. process.addListener('exit', function() {
  46. assert.ok(serverGotConnection);
  47. assert.ok(clientGotOpen);
  48. assert.ok(clientGotData);
  49. assert.ok(clientGotMessage);
  50. assert.ok(serverGotMessage);
  51. assert.ok(serverGotClose);
  52. });