You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1 KiB

  1. var assert = require('assert')
  2. , WebSocket = require('../')
  3. , server = require('./testserver');
  4. var port = 20000;
  5. function getArrayBuffer(buf) {
  6. var l = buf.length;
  7. var arrayBuf = new ArrayBuffer(l);
  8. var uint8View = new Uint8Array(arrayBuf);
  9. for (var i = 0; i < l; i++) {
  10. uint8View[i] = buf[i];
  11. }
  12. return uint8View.buffer;
  13. }
  14. function areArraysEqual(x, y) {
  15. if (x.length != y.length) return false;
  16. for (var i = 0, l = x.length; i < l; ++i) {
  17. if (x[i] !== y[i]) return false;
  18. }
  19. return true;
  20. }
  21. describe('WebSocket', function() {
  22. it('communicates successfully with echo service', function(done) {
  23. var ws = new WebSocket('ws://echo.websocket.org/', {protocolVersion: 13, origin: 'http://websocket.org'});
  24. var str = Date.now().toString();
  25. var dataReceived = false;
  26. ws.on('open', function() {
  27. ws.send(str, {mask: true});
  28. });
  29. ws.on('close', function() {
  30. assert.equal(true, dataReceived);
  31. done();
  32. });
  33. ws.on('message', function(data, flags) {
  34. assert.equal(str, data);
  35. ws.terminate();
  36. dataReceived = true;
  37. });
  38. });
  39. });