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