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.

WSWrapper.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const events_1 = require("events");
  4. class WSWrapper extends events_1.EventEmitter {
  5. constructor(url, _protocols, _websocketOptions) {
  6. super();
  7. this.setMaxListeners(Infinity);
  8. this.ws = new WebSocket(url);
  9. this.ws.onclose = (closeEvent) => {
  10. let reason;
  11. if (closeEvent.reason) {
  12. const enc = new TextEncoder();
  13. reason = enc.encode(closeEvent.reason);
  14. }
  15. this.emit('close', closeEvent.code, reason);
  16. };
  17. this.ws.onopen = () => {
  18. this.emit('open');
  19. };
  20. this.ws.onerror = (error) => {
  21. this.emit('error', error);
  22. };
  23. this.ws.onmessage = (message) => {
  24. this.emit('message', message.data);
  25. };
  26. }
  27. get readyState() {
  28. return this.ws.readyState;
  29. }
  30. close(code, reason) {
  31. if (this.readyState === 1) {
  32. this.ws.close(code, reason);
  33. }
  34. }
  35. send(message) {
  36. this.ws.send(message);
  37. }
  38. }
  39. exports.default = WSWrapper;
  40. WSWrapper.CONNECTING = 0;
  41. WSWrapper.OPEN = 1;
  42. WSWrapper.CLOSING = 2;
  43. WSWrapper.CLOSED = 3;
  44. //# sourceMappingURL=WSWrapper.js.map