socket.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { EventEmitter } from "eventemitter3";
  2. import logger from "./logger";
  3. import { SocketEventType, ServerMessageType } from "./enums";
  4. /**
  5. * An abstraction on top of WebSockets to provide fastest
  6. * possible connection for peers.
  7. */
  8. export class Socket extends EventEmitter {
  9. private _disconnected = false;
  10. private _id?: string;
  11. private _messagesQueue: Array<any> = [];
  12. private _wsUrl: string;
  13. private _socket?: WebSocket;
  14. private _wsPingTimer: any;
  15. constructor(
  16. secure: any,
  17. host: string,
  18. port: number,
  19. path: string,
  20. key: string,
  21. private readonly pingInterval: number = 5000,
  22. ) {
  23. super();
  24. const wsProtocol = secure ? "wss://" : "ws://";
  25. this._wsUrl = wsProtocol + host + ":" + port + path + "peerjs?key=" + key;
  26. }
  27. /** Check in with ID or get one from server. */
  28. start(id: string, token: string): void {
  29. this._id = id;
  30. this._wsUrl += "&id=" + id + "&token=" + token;
  31. this._startWebSocket();
  32. }
  33. /** Start up websocket communications. */
  34. private _startWebSocket(): void {
  35. if (this._socket) {
  36. return;
  37. }
  38. this._socket = new WebSocket(this._wsUrl);
  39. this._socket.onmessage = (event) => {
  40. let data;
  41. try {
  42. data = JSON.parse(event.data);
  43. } catch (e) {
  44. logger.log("Invalid server message", event.data);
  45. return;
  46. }
  47. this.emit(SocketEventType.Message, data);
  48. };
  49. this._socket.onclose = (event) => {
  50. logger.log("Socket closed.", event);
  51. this._disconnected = true;
  52. clearTimeout(this._wsPingTimer);
  53. this.emit(SocketEventType.Disconnected);
  54. };
  55. // Take care of the queue of connections if necessary and make sure Peer knows
  56. // socket is open.
  57. this._socket.onopen = () => {
  58. if (this._disconnected) return;
  59. this._sendQueuedMessages();
  60. logger.log("Socket open");
  61. this._scheduleHeartbeat();
  62. };
  63. }
  64. private _scheduleHeartbeat(): void {
  65. this._wsPingTimer = setTimeout(() => {
  66. this._sendHeartbeat();
  67. }, this.pingInterval);
  68. }
  69. private _sendHeartbeat(): void {
  70. if (!this._wsOpen()) {
  71. logger.log(`Cannot send heartbeat, because socket closed`);
  72. return;
  73. }
  74. const message = JSON.stringify({ type: ServerMessageType.Heartbeat });
  75. this._socket!.send(message);
  76. this._scheduleHeartbeat();
  77. }
  78. /** Is the websocket currently open? */
  79. private _wsOpen(): boolean {
  80. return !!this._socket && this._socket.readyState === 1;
  81. }
  82. /** Send queued messages. */
  83. private _sendQueuedMessages(): void {
  84. //Create copy of queue and clear it,
  85. //because send method push the message back to queue if smth will go wrong
  86. const copiedQueue = [...this._messagesQueue];
  87. this._messagesQueue = [];
  88. for (const message of copiedQueue) {
  89. this.send(message);
  90. }
  91. }
  92. /** Exposed send for DC & Peer. */
  93. send(data: any): void {
  94. if (this._disconnected) {
  95. return;
  96. }
  97. // If we didn't get an ID yet, we can't yet send anything so we should queue
  98. // up these messages.
  99. if (!this._id) {
  100. this._messagesQueue.push(data);
  101. return;
  102. }
  103. if (!data.type) {
  104. this.emit(SocketEventType.Error, "Invalid message");
  105. return;
  106. }
  107. if (!this._wsOpen()) {
  108. return;
  109. }
  110. const message = JSON.stringify(data);
  111. this._socket!.send(message);
  112. }
  113. close(): void {
  114. if (!this._disconnected && !!this._socket) {
  115. this._socket.close();
  116. this._disconnected = true;
  117. clearTimeout(this._wsPingTimer);
  118. }
  119. }
  120. }