mezon-js 2.9.69 → 2.9.71

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