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.
- package/api.gen.ts +30 -7
- package/dist/api.gen.d.ts +19 -5
- package/dist/mezon-js.cjs.js +309 -63
- package/dist/mezon-js.esm.mjs +309 -63
- package/dist/socket.d.ts +59 -1
- package/dist/web_socket_adapter.d.ts +9 -0
- package/package.json +1 -1
- package/socket.ts +748 -225
- package/web_socket_adapter.ts +144 -102
package/web_socket_adapter.ts
CHANGED
@@ -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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
127
|
-
|
135
|
+
value!(message);
|
136
|
+
};
|
137
|
+
} else {
|
138
|
+
value = null;
|
128
139
|
}
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
-
|
145
|
-
|
146
|
-
this._socket = undefined;
|
147
|
-
}
|
199
|
+
this._socket!.send(JSON.stringify(msg));
|
200
|
+
}
|
148
201
|
|
149
|
-
|
150
|
-
|
151
|
-
|
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
|
}
|