baseconnection.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { Peer } from "./peer";
  2. import type { ServerMessage } from "./servermessage";
  3. import type { ConnectionType } from "./enums";
  4. import { BaseConnectionErrorType } from "./enums";
  5. import { PeerError, type PromiseEvents } from "./peerError";
  6. import EventEmitter from "eventemitter3";
  7. import { EventEmitterWithPromise } from "./eventEmitterWithPromise";
  8. export interface BaseConnectionEvents<
  9. ErrorType extends string = BaseConnectionErrorType,
  10. > extends PromiseEvents<never, ErrorType> {
  11. /**
  12. * Emitted when either you or the remote peer closes the connection.
  13. *
  14. * ```ts
  15. * connection.on('close', () => { ... });
  16. * ```
  17. */
  18. close: () => void;
  19. /**
  20. * ```ts
  21. * connection.on('error', (error) => { ... });
  22. * ```
  23. */
  24. error: (error: PeerError<`${ErrorType}`>) => void;
  25. iceStateChanged: (state: RTCIceConnectionState) => void;
  26. }
  27. export interface IBaseConnection<
  28. SubClassEvents extends BaseConnectionEvents<
  29. BaseConnectionErrorType | ErrorType
  30. >,
  31. ErrorType extends string = never,
  32. > extends EventEmitter<SubClassEvents> {
  33. readonly metadata: any;
  34. readonly connectionId: string;
  35. get type(): ConnectionType;
  36. /**
  37. * The optional label passed in or assigned by PeerJS when the connection was initiated.
  38. */
  39. label: string;
  40. /**
  41. * Whether the media connection is active (e.g. your call has been answered).
  42. * You can check this if you want to set a maximum wait time for a one-sided call.
  43. */
  44. get open(): boolean;
  45. close(): void;
  46. }
  47. export abstract class BaseConnection<
  48. AwaitType extends EventEmitter<SubClassEvents>,
  49. SubClassEvents extends BaseConnectionEvents<
  50. BaseConnectionErrorType | ErrorType
  51. >,
  52. ErrorType extends string = never,
  53. >
  54. extends EventEmitterWithPromise<
  55. AwaitType,
  56. never,
  57. ErrorType | BaseConnectionErrorType,
  58. SubClassEvents
  59. >
  60. implements IBaseConnection<SubClassEvents, ErrorType>
  61. {
  62. protected _open = false;
  63. /**
  64. * Any type of metadata associated with the connection,
  65. * passed in by whoever initiated the connection.
  66. */
  67. readonly metadata: any;
  68. connectionId: string;
  69. peerConnection: RTCPeerConnection;
  70. dataChannel: RTCDataChannel;
  71. abstract get type(): ConnectionType;
  72. label: string;
  73. get open() {
  74. return this._open;
  75. }
  76. protected constructor(
  77. /**
  78. * The ID of the peer on the other end of this connection.
  79. */
  80. readonly peer: string,
  81. public provider: Peer,
  82. readonly options: any,
  83. ) {
  84. super();
  85. this.metadata = options.metadata;
  86. }
  87. abstract close(): void;
  88. /**
  89. * @internal
  90. */
  91. abstract handleMessage(message: ServerMessage): void;
  92. /**
  93. * Called by the Negotiator when the DataChannel is ready.
  94. * @internal
  95. * */
  96. abstract _initializeDataChannel(dc: RTCDataChannel): void;
  97. }