mezon-js-protobuf 1.8.75 → 1.8.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/api.ts +806 -137
- package/dist/mezon-js/utils.d.ts +2 -0
- package/dist/mezon-js-protobuf/api/api.d.ts +2203 -1177
- package/dist/mezon-js-protobuf/index.d.ts +1 -0
- package/dist/mezon-js-protobuf/rtapi/realtime.d.ts +2289 -2225
- package/dist/mezon-js-protobuf/utils.d.ts +4 -1
- package/dist/mezon-js-protobuf.cjs.js +705 -127
- package/dist/mezon-js-protobuf.esm.mjs +705 -127
- package/index.ts +1 -0
- package/package.json +1 -1
- package/rtapi/realtime.ts +97 -27
- package/utils.ts +33 -0
- package/web_socket_adapter_pb.ts +123 -137
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mezon-js-protobuf",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.77",
|
|
4
4
|
"description": "Websocket adapter adding protocol buffer support to the Mezon Javascript client.",
|
|
5
5
|
"main": "dist/mezon-js-protobuf.cjs.js",
|
|
6
6
|
"module": "dist/mezon-js-protobuf.esm.mjs",
|
package/rtapi/realtime.ts
CHANGED
|
@@ -38,6 +38,8 @@ import {
|
|
|
38
38
|
ListChannelDetailRequest,
|
|
39
39
|
ListChannelMessagesRequest,
|
|
40
40
|
ListChannelUsersRequest,
|
|
41
|
+
ListClanBadgeCountRequest,
|
|
42
|
+
ListClanBadgeCountResponse,
|
|
41
43
|
ListClanDescRequest,
|
|
42
44
|
ListClanUnreadMsgIndicatorRequest,
|
|
43
45
|
ListClanUnreadMsgIndicatorResponse,
|
|
@@ -794,9 +796,9 @@ export interface ChannelMessageRemove {
|
|
|
794
796
|
/** */
|
|
795
797
|
topic_id: string;
|
|
796
798
|
/** Message mention */
|
|
797
|
-
mentions:
|
|
799
|
+
mentions: Uint8Array;
|
|
798
800
|
/** Message reference */
|
|
799
|
-
references:
|
|
801
|
+
references: Uint8Array;
|
|
800
802
|
}
|
|
801
803
|
|
|
802
804
|
/** A set of joins and leaves on a particular channel. */
|
|
@@ -1743,6 +1745,8 @@ export interface ListDataSocket {
|
|
|
1743
1745
|
stream_user_list: StreamingChannelUserList | undefined;
|
|
1744
1746
|
list_unread_msg_indicator_req: ListClanUnreadMsgIndicatorRequest | undefined;
|
|
1745
1747
|
unread_msg_indicator: ListClanUnreadMsgIndicatorResponse | undefined;
|
|
1748
|
+
list_clan_badge_count_req: ListClanBadgeCountRequest | undefined;
|
|
1749
|
+
clan_badge_count: ListClanBadgeCountResponse | undefined;
|
|
1746
1750
|
}
|
|
1747
1751
|
|
|
1748
1752
|
export interface MeetParticipantEvent {
|
|
@@ -1773,7 +1777,7 @@ export interface FcmDataPayload {
|
|
|
1773
1777
|
command_type: number;
|
|
1774
1778
|
receiver_id: string;
|
|
1775
1779
|
title: string;
|
|
1776
|
-
body:
|
|
1780
|
+
body: Uint8Array;
|
|
1777
1781
|
user_role_ids: string[];
|
|
1778
1782
|
user_sent_ids: string[];
|
|
1779
1783
|
priority: number;
|
|
@@ -6663,8 +6667,8 @@ function createBaseChannelMessageRemove(): ChannelMessageRemove {
|
|
|
6663
6667
|
is_public: false,
|
|
6664
6668
|
has_attachment: false,
|
|
6665
6669
|
topic_id: "",
|
|
6666
|
-
mentions:
|
|
6667
|
-
references:
|
|
6670
|
+
mentions: new Uint8Array(0),
|
|
6671
|
+
references: new Uint8Array(0),
|
|
6668
6672
|
};
|
|
6669
6673
|
}
|
|
6670
6674
|
|
|
@@ -6691,11 +6695,11 @@ export const ChannelMessageRemove = {
|
|
|
6691
6695
|
if (message.topic_id !== "") {
|
|
6692
6696
|
writer.uint32(58).string(message.topic_id);
|
|
6693
6697
|
}
|
|
6694
|
-
if (message.mentions !==
|
|
6695
|
-
writer.uint32(66).
|
|
6698
|
+
if (message.mentions.length !== 0) {
|
|
6699
|
+
writer.uint32(66).bytes(message.mentions);
|
|
6696
6700
|
}
|
|
6697
|
-
if (message.references !==
|
|
6698
|
-
writer.uint32(74).
|
|
6701
|
+
if (message.references.length !== 0) {
|
|
6702
|
+
writer.uint32(74).bytes(message.references);
|
|
6699
6703
|
}
|
|
6700
6704
|
return writer;
|
|
6701
6705
|
},
|
|
@@ -6761,14 +6765,14 @@ export const ChannelMessageRemove = {
|
|
|
6761
6765
|
break;
|
|
6762
6766
|
}
|
|
6763
6767
|
|
|
6764
|
-
message.mentions = reader.
|
|
6768
|
+
message.mentions = reader.bytes();
|
|
6765
6769
|
continue;
|
|
6766
6770
|
case 9:
|
|
6767
6771
|
if (tag !== 74) {
|
|
6768
6772
|
break;
|
|
6769
6773
|
}
|
|
6770
6774
|
|
|
6771
|
-
message.references = reader.
|
|
6775
|
+
message.references = reader.bytes();
|
|
6772
6776
|
continue;
|
|
6773
6777
|
}
|
|
6774
6778
|
if ((tag & 7) === 4 || tag === 0) {
|
|
@@ -6788,8 +6792,8 @@ export const ChannelMessageRemove = {
|
|
|
6788
6792
|
is_public: isSet(object.is_public) ? globalThis.Boolean(object.is_public) : false,
|
|
6789
6793
|
has_attachment: isSet(object.has_attachment) ? globalThis.Boolean(object.has_attachment) : false,
|
|
6790
6794
|
topic_id: isSet(object.topic_id) ? globalThis.String(object.topic_id) : "",
|
|
6791
|
-
mentions: isSet(object.mentions) ?
|
|
6792
|
-
references: isSet(object.references) ?
|
|
6795
|
+
mentions: isSet(object.mentions) ? bytesFromBase64(object.mentions) : new Uint8Array(0),
|
|
6796
|
+
references: isSet(object.references) ? bytesFromBase64(object.references) : new Uint8Array(0),
|
|
6793
6797
|
};
|
|
6794
6798
|
},
|
|
6795
6799
|
|
|
@@ -6816,11 +6820,11 @@ export const ChannelMessageRemove = {
|
|
|
6816
6820
|
if (message.topic_id !== "") {
|
|
6817
6821
|
obj.topic_id = message.topic_id;
|
|
6818
6822
|
}
|
|
6819
|
-
if (message.mentions !==
|
|
6820
|
-
obj.mentions = message.mentions;
|
|
6823
|
+
if (message.mentions.length !== 0) {
|
|
6824
|
+
obj.mentions = base64FromBytes(message.mentions);
|
|
6821
6825
|
}
|
|
6822
|
-
if (message.references !==
|
|
6823
|
-
obj.references = message.references;
|
|
6826
|
+
if (message.references.length !== 0) {
|
|
6827
|
+
obj.references = base64FromBytes(message.references);
|
|
6824
6828
|
}
|
|
6825
6829
|
return obj;
|
|
6826
6830
|
},
|
|
@@ -6837,8 +6841,8 @@ export const ChannelMessageRemove = {
|
|
|
6837
6841
|
message.is_public = object.is_public ?? false;
|
|
6838
6842
|
message.has_attachment = object.has_attachment ?? false;
|
|
6839
6843
|
message.topic_id = object.topic_id ?? "";
|
|
6840
|
-
message.mentions = object.mentions ??
|
|
6841
|
-
message.references = object.references ??
|
|
6844
|
+
message.mentions = object.mentions ?? new Uint8Array(0);
|
|
6845
|
+
message.references = object.references ?? new Uint8Array(0);
|
|
6842
6846
|
return message;
|
|
6843
6847
|
},
|
|
6844
6848
|
};
|
|
@@ -14673,6 +14677,8 @@ function createBaseListDataSocket(): ListDataSocket {
|
|
|
14673
14677
|
stream_user_list: undefined,
|
|
14674
14678
|
list_unread_msg_indicator_req: undefined,
|
|
14675
14679
|
unread_msg_indicator: undefined,
|
|
14680
|
+
list_clan_badge_count_req: undefined,
|
|
14681
|
+
clan_badge_count: undefined,
|
|
14676
14682
|
};
|
|
14677
14683
|
}
|
|
14678
14684
|
|
|
@@ -14878,6 +14884,12 @@ export const ListDataSocket = {
|
|
|
14878
14884
|
if (message.unread_msg_indicator !== undefined) {
|
|
14879
14885
|
ListClanUnreadMsgIndicatorResponse.encode(message.unread_msg_indicator, writer.uint32(530).fork()).ldelim();
|
|
14880
14886
|
}
|
|
14887
|
+
if (message.list_clan_badge_count_req !== undefined) {
|
|
14888
|
+
ListClanBadgeCountRequest.encode(message.list_clan_badge_count_req, writer.uint32(538).fork()).ldelim();
|
|
14889
|
+
}
|
|
14890
|
+
if (message.clan_badge_count !== undefined) {
|
|
14891
|
+
ListClanBadgeCountResponse.encode(message.clan_badge_count, writer.uint32(546).fork()).ldelim();
|
|
14892
|
+
}
|
|
14881
14893
|
return writer;
|
|
14882
14894
|
},
|
|
14883
14895
|
|
|
@@ -15353,6 +15365,20 @@ export const ListDataSocket = {
|
|
|
15353
15365
|
|
|
15354
15366
|
message.unread_msg_indicator = ListClanUnreadMsgIndicatorResponse.decode(reader, reader.uint32());
|
|
15355
15367
|
continue;
|
|
15368
|
+
case 67:
|
|
15369
|
+
if (tag !== 538) {
|
|
15370
|
+
break;
|
|
15371
|
+
}
|
|
15372
|
+
|
|
15373
|
+
message.list_clan_badge_count_req = ListClanBadgeCountRequest.decode(reader, reader.uint32());
|
|
15374
|
+
continue;
|
|
15375
|
+
case 68:
|
|
15376
|
+
if (tag !== 546) {
|
|
15377
|
+
break;
|
|
15378
|
+
}
|
|
15379
|
+
|
|
15380
|
+
message.clan_badge_count = ListClanBadgeCountResponse.decode(reader, reader.uint32());
|
|
15381
|
+
continue;
|
|
15356
15382
|
}
|
|
15357
15383
|
if ((tag & 7) === 4 || tag === 0) {
|
|
15358
15384
|
break;
|
|
@@ -15518,6 +15544,12 @@ export const ListDataSocket = {
|
|
|
15518
15544
|
unread_msg_indicator: isSet(object.unread_msg_indicator)
|
|
15519
15545
|
? ListClanUnreadMsgIndicatorResponse.fromJSON(object.unread_msg_indicator)
|
|
15520
15546
|
: undefined,
|
|
15547
|
+
list_clan_badge_count_req: isSet(object.list_clan_badge_count_req)
|
|
15548
|
+
? ListClanBadgeCountRequest.fromJSON(object.list_clan_badge_count_req)
|
|
15549
|
+
: undefined,
|
|
15550
|
+
clan_badge_count: isSet(object.clan_badge_count)
|
|
15551
|
+
? ListClanBadgeCountResponse.fromJSON(object.clan_badge_count)
|
|
15552
|
+
: undefined,
|
|
15521
15553
|
};
|
|
15522
15554
|
},
|
|
15523
15555
|
|
|
@@ -15725,6 +15757,12 @@ export const ListDataSocket = {
|
|
|
15725
15757
|
if (message.unread_msg_indicator !== undefined) {
|
|
15726
15758
|
obj.unread_msg_indicator = ListClanUnreadMsgIndicatorResponse.toJSON(message.unread_msg_indicator);
|
|
15727
15759
|
}
|
|
15760
|
+
if (message.list_clan_badge_count_req !== undefined) {
|
|
15761
|
+
obj.list_clan_badge_count_req = ListClanBadgeCountRequest.toJSON(message.list_clan_badge_count_req);
|
|
15762
|
+
}
|
|
15763
|
+
if (message.clan_badge_count !== undefined) {
|
|
15764
|
+
obj.clan_badge_count = ListClanBadgeCountResponse.toJSON(message.clan_badge_count);
|
|
15765
|
+
}
|
|
15728
15766
|
return obj;
|
|
15729
15767
|
},
|
|
15730
15768
|
|
|
@@ -15942,6 +15980,13 @@ export const ListDataSocket = {
|
|
|
15942
15980
|
message.unread_msg_indicator = (object.unread_msg_indicator !== undefined && object.unread_msg_indicator !== null)
|
|
15943
15981
|
? ListClanUnreadMsgIndicatorResponse.fromPartial(object.unread_msg_indicator)
|
|
15944
15982
|
: undefined;
|
|
15983
|
+
message.list_clan_badge_count_req =
|
|
15984
|
+
(object.list_clan_badge_count_req !== undefined && object.list_clan_badge_count_req !== null)
|
|
15985
|
+
? ListClanBadgeCountRequest.fromPartial(object.list_clan_badge_count_req)
|
|
15986
|
+
: undefined;
|
|
15987
|
+
message.clan_badge_count = (object.clan_badge_count !== undefined && object.clan_badge_count !== null)
|
|
15988
|
+
? ListClanBadgeCountResponse.fromPartial(object.clan_badge_count)
|
|
15989
|
+
: undefined;
|
|
15945
15990
|
return message;
|
|
15946
15991
|
},
|
|
15947
15992
|
};
|
|
@@ -16307,7 +16352,7 @@ function createBaseFcmDataPayload(): FcmDataPayload {
|
|
|
16307
16352
|
command_type: 0,
|
|
16308
16353
|
receiver_id: "",
|
|
16309
16354
|
title: "",
|
|
16310
|
-
body:
|
|
16355
|
+
body: new Uint8Array(0),
|
|
16311
16356
|
user_role_ids: [],
|
|
16312
16357
|
user_sent_ids: [],
|
|
16313
16358
|
priority: 0,
|
|
@@ -16332,8 +16377,8 @@ export const FcmDataPayload = {
|
|
|
16332
16377
|
if (message.title !== "") {
|
|
16333
16378
|
writer.uint32(26).string(message.title);
|
|
16334
16379
|
}
|
|
16335
|
-
if (message.body !==
|
|
16336
|
-
writer.uint32(34).
|
|
16380
|
+
if (message.body.length !== 0) {
|
|
16381
|
+
writer.uint32(34).bytes(message.body);
|
|
16337
16382
|
}
|
|
16338
16383
|
for (const v of message.user_role_ids) {
|
|
16339
16384
|
writer.uint32(42).string(v!);
|
|
@@ -16401,7 +16446,7 @@ export const FcmDataPayload = {
|
|
|
16401
16446
|
break;
|
|
16402
16447
|
}
|
|
16403
16448
|
|
|
16404
|
-
message.body = reader.
|
|
16449
|
+
message.body = reader.bytes();
|
|
16405
16450
|
continue;
|
|
16406
16451
|
case 5:
|
|
16407
16452
|
if (tag !== 42) {
|
|
@@ -16487,7 +16532,7 @@ export const FcmDataPayload = {
|
|
|
16487
16532
|
command_type: isSet(object.command_type) ? globalThis.Number(object.command_type) : 0,
|
|
16488
16533
|
receiver_id: isSet(object.receiver_id) ? globalThis.String(object.receiver_id) : "",
|
|
16489
16534
|
title: isSet(object.title) ? globalThis.String(object.title) : "",
|
|
16490
|
-
body: isSet(object.body) ?
|
|
16535
|
+
body: isSet(object.body) ? bytesFromBase64(object.body) : new Uint8Array(0),
|
|
16491
16536
|
user_role_ids: globalThis.Array.isArray(object?.user_role_ids)
|
|
16492
16537
|
? object.user_role_ids.map((e: any) => globalThis.String(e))
|
|
16493
16538
|
: [],
|
|
@@ -16522,8 +16567,8 @@ export const FcmDataPayload = {
|
|
|
16522
16567
|
if (message.title !== "") {
|
|
16523
16568
|
obj.title = message.title;
|
|
16524
16569
|
}
|
|
16525
|
-
if (message.body !==
|
|
16526
|
-
obj.body = message.body;
|
|
16570
|
+
if (message.body.length !== 0) {
|
|
16571
|
+
obj.body = base64FromBytes(message.body);
|
|
16527
16572
|
}
|
|
16528
16573
|
if (message.user_role_ids?.length) {
|
|
16529
16574
|
obj.user_role_ids = message.user_role_ids;
|
|
@@ -16566,7 +16611,7 @@ export const FcmDataPayload = {
|
|
|
16566
16611
|
message.command_type = object.command_type ?? 0;
|
|
16567
16612
|
message.receiver_id = object.receiver_id ?? "";
|
|
16568
16613
|
message.title = object.title ?? "";
|
|
16569
|
-
message.body = object.body ??
|
|
16614
|
+
message.body = object.body ?? new Uint8Array(0);
|
|
16570
16615
|
message.user_role_ids = object.user_role_ids?.map((e) => e) || [];
|
|
16571
16616
|
message.user_sent_ids = object.user_sent_ids?.map((e) => e) || [];
|
|
16572
16617
|
message.priority = object.priority ?? 0;
|
|
@@ -16583,6 +16628,31 @@ export const FcmDataPayload = {
|
|
|
16583
16628
|
},
|
|
16584
16629
|
};
|
|
16585
16630
|
|
|
16631
|
+
function bytesFromBase64(b64: string): Uint8Array {
|
|
16632
|
+
if ((globalThis as any).Buffer) {
|
|
16633
|
+
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
|
|
16634
|
+
} else {
|
|
16635
|
+
const bin = globalThis.atob(b64);
|
|
16636
|
+
const arr = new Uint8Array(bin.length);
|
|
16637
|
+
for (let i = 0; i < bin.length; ++i) {
|
|
16638
|
+
arr[i] = bin.charCodeAt(i);
|
|
16639
|
+
}
|
|
16640
|
+
return arr;
|
|
16641
|
+
}
|
|
16642
|
+
}
|
|
16643
|
+
|
|
16644
|
+
function base64FromBytes(arr: Uint8Array): string {
|
|
16645
|
+
if ((globalThis as any).Buffer) {
|
|
16646
|
+
return globalThis.Buffer.from(arr).toString("base64");
|
|
16647
|
+
} else {
|
|
16648
|
+
const bin: string[] = [];
|
|
16649
|
+
arr.forEach((byte) => {
|
|
16650
|
+
bin.push(globalThis.String.fromCharCode(byte));
|
|
16651
|
+
});
|
|
16652
|
+
return globalThis.btoa(bin.join(""));
|
|
16653
|
+
}
|
|
16654
|
+
}
|
|
16655
|
+
|
|
16586
16656
|
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
|
|
16587
16657
|
|
|
16588
16658
|
export type DeepPartial<T> = T extends Builtin ? T
|
package/utils.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as tsproto from "./api/api";
|
|
2
|
+
|
|
3
|
+
export function decodeMentions(data: any) {
|
|
4
|
+
const buffer: ArrayBuffer = data;
|
|
5
|
+
const uintBuffer: Uint8Array = new Uint8Array(buffer);
|
|
6
|
+
const mentions = tsproto.MessageMentionList.decode(uintBuffer);
|
|
7
|
+
|
|
8
|
+
return mentions
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function decodeAttachments(data: any) {
|
|
12
|
+
const buffer: ArrayBuffer = data;
|
|
13
|
+
const uintBuffer: Uint8Array = new Uint8Array(buffer);
|
|
14
|
+
const attachments = tsproto.MessageAttachmentList.decode(uintBuffer);
|
|
15
|
+
|
|
16
|
+
return attachments
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function decodeRefs(data: any) {
|
|
20
|
+
const buffer: ArrayBuffer = data;
|
|
21
|
+
const uintBuffer: Uint8Array = new Uint8Array(buffer);
|
|
22
|
+
const refs = tsproto.MessageRefList.decode(uintBuffer);
|
|
23
|
+
|
|
24
|
+
return refs
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function decodeNotificationFcm(data: any) {
|
|
28
|
+
const buffer: ArrayBuffer = data;
|
|
29
|
+
const uintBuffer: Uint8Array = new Uint8Array(buffer);
|
|
30
|
+
const noti = tsproto.DirectFcmProto.decode(uintBuffer);
|
|
31
|
+
|
|
32
|
+
return noti
|
|
33
|
+
}
|
package/web_socket_adapter_pb.ts
CHANGED
|
@@ -1,137 +1,123 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2020 The Mezon Authors
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import {
|
|
17
|
-
WebSocketAdapter,
|
|
18
|
-
SocketCloseHandler,
|
|
19
|
-
SocketErrorHandler,
|
|
20
|
-
SocketMessageHandler,
|
|
21
|
-
SocketOpenHandler,
|
|
22
|
-
} from "../mezon-js/web_socket_adapter";
|
|
23
|
-
import * as tsproto from "./rtapi/realtime";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A protocol buffer socket adapter that accepts and transmits payloads using the protobuf binary wire format.
|
|
27
|
-
*/
|
|
28
|
-
export class WebSocketAdapterPb implements WebSocketAdapter {
|
|
29
|
-
private _socket?: WebSocket;
|
|
30
|
-
|
|
31
|
-
constructor() {
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get onClose(): SocketCloseHandler | null {
|
|
35
|
-
return this._socket!.onclose;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
set onClose(value: SocketCloseHandler | null) {
|
|
39
|
-
this._socket!.onclose = value;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get onError(): SocketErrorHandler | null {
|
|
43
|
-
return this._socket!.onerror;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
set onError(value: SocketErrorHandler | null) {
|
|
47
|
-
this._socket!.onerror = value;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get onMessage(): SocketMessageHandler | null {
|
|
51
|
-
return this._socket!.onmessage;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
set onMessage(value: SocketMessageHandler | null) {
|
|
55
|
-
if (value) {
|
|
56
|
-
this._socket!.onmessage = (evt: MessageEvent) => {
|
|
57
|
-
const buffer: ArrayBuffer = evt.data;
|
|
58
|
-
const uintBuffer: Uint8Array = new Uint8Array(buffer);
|
|
59
|
-
const envelope = tsproto.Envelope.decode(uintBuffer);
|
|
60
|
-
|
|
61
|
-
if (envelope.channel_message) {
|
|
62
|
-
if (envelope.channel_message.code == undefined) {
|
|
63
|
-
//protobuf plugin does not default-initialize missing Int32Value fields
|
|
64
|
-
envelope.channel_message.code = 0;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
value!(envelope);
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
value = null;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get onOpen(): SocketOpenHandler | null {
|
|
77
|
-
return this._socket!.onopen;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
set onOpen(value: SocketOpenHandler | null) {
|
|
81
|
-
this._socket!.onopen = value;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
isOpen(): boolean {
|
|
85
|
-
return this._socket?.readyState == WebSocket.OPEN;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
close() {
|
|
89
|
-
this._socket?.close();
|
|
90
|
-
this._socket = undefined;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
connect(
|
|
94
|
-
scheme: string,
|
|
95
|
-
host: string,
|
|
96
|
-
port: string,
|
|
97
|
-
createStatus: boolean,
|
|
98
|
-
token: string,
|
|
99
|
-
platform: string,
|
|
100
|
-
signal?: AbortSignal
|
|
101
|
-
): void {
|
|
102
|
-
if (signal) {
|
|
103
|
-
signal.addEventListener("abort", () => {
|
|
104
|
-
this.close();
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(
|
|
108
|
-
createStatus.toString()
|
|
109
|
-
)}&token=${encodeURIComponent(
|
|
110
|
-
token
|
|
111
|
-
)}&format=protobuf&platform=${encodeURIComponent(platform)}`;
|
|
112
|
-
this._socket = new WebSocket(url);
|
|
113
|
-
this._socket.binaryType = "arraybuffer";
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
send(msg: any): void {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
let payload = msg.party_data_send.data;
|
|
125
|
-
// can't send a string over protobuf
|
|
126
|
-
if (typeof payload == "string") {
|
|
127
|
-
msg.party_data_send.data = new TextEncoder().encode(payload);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const envelopeWriter = tsproto.Envelope.encode(
|
|
132
|
-
tsproto.Envelope.fromPartial(msg)
|
|
133
|
-
);
|
|
134
|
-
const encodedMsg = envelopeWriter.finish();
|
|
135
|
-
this._socket!.send(encodedMsg);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2020 The Mezon Authors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
WebSocketAdapter,
|
|
18
|
+
SocketCloseHandler,
|
|
19
|
+
SocketErrorHandler,
|
|
20
|
+
SocketMessageHandler,
|
|
21
|
+
SocketOpenHandler,
|
|
22
|
+
} from "../mezon-js/web_socket_adapter";
|
|
23
|
+
import * as tsproto from "./rtapi/realtime";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A protocol buffer socket adapter that accepts and transmits payloads using the protobuf binary wire format.
|
|
27
|
+
*/
|
|
28
|
+
export class WebSocketAdapterPb implements WebSocketAdapter {
|
|
29
|
+
private _socket?: WebSocket;
|
|
30
|
+
|
|
31
|
+
constructor() {
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get onClose(): SocketCloseHandler | null {
|
|
35
|
+
return this._socket!.onclose;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
set onClose(value: SocketCloseHandler | null) {
|
|
39
|
+
this._socket!.onclose = value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get onError(): SocketErrorHandler | null {
|
|
43
|
+
return this._socket!.onerror;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
set onError(value: SocketErrorHandler | null) {
|
|
47
|
+
this._socket!.onerror = value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get onMessage(): SocketMessageHandler | null {
|
|
51
|
+
return this._socket!.onmessage;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
set onMessage(value: SocketMessageHandler | null) {
|
|
55
|
+
if (value) {
|
|
56
|
+
this._socket!.onmessage = (evt: MessageEvent) => {
|
|
57
|
+
const buffer: ArrayBuffer = evt.data;
|
|
58
|
+
const uintBuffer: Uint8Array = new Uint8Array(buffer);
|
|
59
|
+
const envelope = tsproto.Envelope.decode(uintBuffer);
|
|
60
|
+
|
|
61
|
+
if (envelope.channel_message) {
|
|
62
|
+
if (envelope.channel_message.code == undefined) {
|
|
63
|
+
//protobuf plugin does not default-initialize missing Int32Value fields
|
|
64
|
+
envelope.channel_message.code = 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
value!(envelope);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
value = null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get onOpen(): SocketOpenHandler | null {
|
|
77
|
+
return this._socket!.onopen;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
set onOpen(value: SocketOpenHandler | null) {
|
|
81
|
+
this._socket!.onopen = value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
isOpen(): boolean {
|
|
85
|
+
return this._socket?.readyState == WebSocket.OPEN;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
close() {
|
|
89
|
+
this._socket?.close();
|
|
90
|
+
this._socket = undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
connect(
|
|
94
|
+
scheme: string,
|
|
95
|
+
host: string,
|
|
96
|
+
port: string,
|
|
97
|
+
createStatus: boolean,
|
|
98
|
+
token: string,
|
|
99
|
+
platform: string,
|
|
100
|
+
signal?: AbortSignal
|
|
101
|
+
): void {
|
|
102
|
+
if (signal) {
|
|
103
|
+
signal.addEventListener("abort", () => {
|
|
104
|
+
this.close();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(
|
|
108
|
+
createStatus.toString()
|
|
109
|
+
)}&token=${encodeURIComponent(
|
|
110
|
+
token
|
|
111
|
+
)}&format=protobuf&platform=${encodeURIComponent(platform)}`;
|
|
112
|
+
this._socket = new WebSocket(url);
|
|
113
|
+
this._socket.binaryType = "arraybuffer";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
send(msg: any): void {
|
|
117
|
+
const envelopeWriter = tsproto.Envelope.encode(
|
|
118
|
+
tsproto.Envelope.fromPartial(msg)
|
|
119
|
+
);
|
|
120
|
+
const encodedMsg = envelopeWriter.finish();
|
|
121
|
+
this._socket!.send(encodedMsg);
|
|
122
|
+
}
|
|
123
|
+
}
|