mezon-js 2.9.68 → 2.9.70

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,44 +15,54 @@
15
15
  */
16
16
 
17
17
  import { decode, encode } from "base64-arraybuffer";
18
- import { btoa } from "js-base64"
18
+ import { btoa } from "js-base64";
19
19
 
20
20
  /**
21
21
  * An interface used by Mezon's web socket to determine the payload protocol.
22
22
  */
23
23
  export interface WebSocketAdapter {
24
-
25
- /**
26
- * Dispatched when the web socket closes.
27
- */
28
- onClose: SocketCloseHandler | null;
29
-
30
- /**
31
- * Dispatched when the web socket receives an error.
32
- */
33
- onError: SocketErrorHandler | null;
34
-
35
- /**
36
- * Dispatched when the web socket receives a normal message.
37
- */
38
- onMessage: SocketMessageHandler | null;
39
-
40
- /**
41
- * Dispatched when the web socket opens.
42
- */
43
- onOpen: SocketOpenHandler | null;
44
-
45
- isOpen(): boolean;
46
- close(): void;
47
- connect(scheme: string, host: string, port: string, createStatus: boolean, token: string, platform: string, signal?: AbortSignal): void;
48
- send(message: any): void;
24
+ /**
25
+ * Dispatched when the web socket closes.
26
+ */
27
+ onClose: SocketCloseHandler | null;
28
+
29
+ /**
30
+ * Dispatched when the web socket receives an error.
31
+ */
32
+ onError: SocketErrorHandler | null;
33
+
34
+ /**
35
+ * Dispatched when the web socket receives a normal message.
36
+ */
37
+ onMessage: SocketMessageHandler | null;
38
+
39
+ /**
40
+ * Dispatched when the web socket opens.
41
+ */
42
+ onOpen: SocketOpenHandler | null;
43
+
44
+ isOpen(): boolean;
45
+ close(): void;
46
+ connect(
47
+ scheme: string,
48
+ host: string,
49
+ port: string,
50
+ createStatus: boolean,
51
+ token: string,
52
+ platform: string,
53
+ signal?: AbortSignal
54
+ ): void;
55
+ send(message: any): void;
56
+
57
+ // RTC Peer Connection
58
+ getRTCPeerConnection(): RTCPeerConnection;
49
59
  }
50
60
 
51
61
  /**
52
62
  * SocketCloseHandler defines a lambda that handles WebSocket close events.
53
63
  */
54
64
  export interface SocketCloseHandler {
55
- (this: WebSocket, evt: CloseEvent): void;
65
+ (this: WebSocket, evt: CloseEvent): void;
56
66
  }
57
67
 
58
68
  /**
@@ -60,104 +70,136 @@ export interface SocketCloseHandler {
60
70
  * that indicate an error.
61
71
  */
62
72
  export interface SocketErrorHandler {
63
- (this: WebSocket, evt: Event): void;
73
+ (this: WebSocket, evt: Event): void;
64
74
  }
65
75
 
66
76
  /**
67
77
  * SocketMessageHandler defines a lambda that handles valid WebSocket messages.
68
78
  */
69
79
  export interface SocketMessageHandler {
70
- (message: any): void;
80
+ (message: any): void;
71
81
  }
72
82
 
73
83
  /**
74
84
  * SocketOpenHandler defines a lambda that handles WebSocket open events.
75
85
  */
76
86
  export interface SocketOpenHandler {
77
- (this: WebSocket, evt: Event): void
87
+ (this: WebSocket, evt: Event): void;
88
+ }
89
+
90
+ export interface RTCPeerOntrackHandler {
91
+ (this: RTCPeerConnection, ev: RTCTrackEvent): any | null;
92
+ }
93
+
94
+ export interface RTCPeerOnicecandidateHandler {
95
+ (this: RTCPeerConnection, ev: RTCPeerConnectionIceEvent): any;
78
96
  }
79
97
 
80
98
  /**
81
99
  * A text-based socket adapter that accepts and transmits payloads over UTF-8.
82
100
  */
83
101
  export class WebSocketAdapterText implements WebSocketAdapter {
84
- private _socket?: WebSocket;
85
-
86
- get onClose(): SocketCloseHandler | null {
87
- return this._socket!.onclose;
88
- }
89
-
90
- set onClose(value: SocketCloseHandler | null) {
91
- this._socket!.onclose = value;
92
- }
93
-
94
- get onError(): SocketErrorHandler | null {
95
- return this._socket!.onerror;
96
- }
97
-
98
- set onError(value: SocketErrorHandler | null) {
99
- this._socket!.onerror = value;
100
- }
101
-
102
- get onMessage(): SocketMessageHandler | null {
103
- return this._socket!.onmessage;
104
- }
105
-
106
- set onMessage(value: SocketMessageHandler | null) {
107
- if (value) {
108
- this._socket!.onmessage = (evt: MessageEvent) => {
109
- const message: any = JSON.parse(evt.data);
110
- if (message.party_data && message.party_data.data) {
111
- message.party_data.data = new Uint8Array(decode(message.party_data.data));
112
- }
113
-
114
- value!(message);
115
- };
116
- }
117
- else {
118
- value = null;
102
+ private _socket?: WebSocket;
103
+ private _pcRef?: RTCPeerConnection;
104
+
105
+ get onClose(): SocketCloseHandler | null {
106
+ return this._socket!.onclose;
107
+ }
108
+
109
+ set onClose(value: SocketCloseHandler | null) {
110
+ this._socket!.onclose = value;
111
+ }
112
+
113
+ get onError(): SocketErrorHandler | null {
114
+ return this._socket!.onerror;
115
+ }
116
+
117
+ set onError(value: SocketErrorHandler | null) {
118
+ this._socket!.onerror = value;
119
+ }
120
+
121
+ get onMessage(): SocketMessageHandler | null {
122
+ return this._socket!.onmessage;
123
+ }
124
+
125
+ set onMessage(value: SocketMessageHandler | null) {
126
+ if (value) {
127
+ this._socket!.onmessage = (evt: MessageEvent) => {
128
+ const message: any = JSON.parse(evt.data);
129
+ if (message.party_data && message.party_data.data) {
130
+ message.party_data.data = new Uint8Array(
131
+ decode(message.party_data.data)
132
+ );
119
133
  }
120
- }
121
-
122
- get onOpen(): SocketOpenHandler | null {
123
- return this._socket!.onopen;
124
- }
125
134
 
126
- set onOpen(value: SocketOpenHandler | null) {
127
- this._socket!.onopen = value;
135
+ value!(message);
136
+ };
137
+ } else {
138
+ value = null;
128
139
  }
129
-
130
- isOpen(): boolean {
131
- return this._socket?.readyState == WebSocket.OPEN;
140
+ }
141
+
142
+ get onOpen(): SocketOpenHandler | null {
143
+ return this._socket!.onopen;
144
+ }
145
+
146
+ set onOpen(value: SocketOpenHandler | null) {
147
+ this._socket!.onopen = value;
148
+ }
149
+
150
+ isOpen(): boolean {
151
+ return this._socket?.readyState == WebSocket.OPEN;
152
+ }
153
+
154
+ connect(
155
+ scheme: string,
156
+ host: string,
157
+ port: string,
158
+ createStatus: boolean,
159
+ token: string,
160
+ platform: string,
161
+ signal?: AbortSignal
162
+ ): void {
163
+ if (signal) {
164
+ signal.addEventListener("abort", () => {
165
+ this.close();
166
+ });
132
167
  }
133
-
134
- connect(scheme: string, host: string, port: string, createStatus: boolean, token: string, platform: string, signal?: AbortSignal): void {
135
- if (signal) {
136
- signal.addEventListener('abort', () => {
137
- this.close();
138
- });
139
- }
140
- const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(createStatus.toString())}&token=${encodeURIComponent(token)}&paltform=${encodeURIComponent(platform)}`;
141
- this._socket = new WebSocket(url);
168
+ const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(
169
+ createStatus.toString()
170
+ )}&token=${encodeURIComponent(token)}&paltform=${encodeURIComponent(
171
+ platform
172
+ )}`;
173
+ this._socket = new WebSocket(url);
174
+ this._pcRef = new RTCPeerConnection({
175
+ iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
176
+ });
177
+ }
178
+
179
+ close() {
180
+ this._pcRef?.close();
181
+ this._pcRef = undefined;
182
+ this._socket?.close();
183
+ this._socket = undefined;
184
+ }
185
+
186
+ send(msg: any): void {
187
+ if (msg.party_data_send) {
188
+ // according to protobuf docs, int64 is encoded to JSON as string.
189
+ msg.party_data_send.op_code = msg.party_data_send.op_code.toString();
190
+ let payload = msg.party_data_send.data;
191
+ if (payload && payload instanceof Uint8Array) {
192
+ msg.party_data_send.data = encode(payload.buffer);
193
+ } else if (payload) {
194
+ // it's a string
195
+ msg.party_data_send.data = btoa(payload);
196
+ }
142
197
  }
143
198
 
144
- close() {
145
- this._socket?.close();
146
- this._socket = undefined;
147
- }
199
+ this._socket!.send(JSON.stringify(msg));
200
+ }
148
201
 
149
- send(msg: any): void {
150
- if (msg.party_data_send) {
151
- // according to protobuf docs, int64 is encoded to JSON as string.
152
- msg.party_data_send.op_code = msg.party_data_send.op_code.toString();
153
- let payload = msg.party_data_send.data;
154
- if (payload && payload instanceof Uint8Array) {
155
- msg.party_data_send.data = encode(payload.buffer);
156
- } else if (payload) { // it's a string
157
- msg.party_data_send.data = btoa(payload);
158
- }
159
- }
160
-
161
- this._socket!.send(JSON.stringify(msg));
162
- }
202
+ getRTCPeerConnection(): RTCPeerConnection {
203
+ return this._pcRef!;
204
+ }
163
205
  }