@telperion/messenger 8.0.0 → 8.1.0
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/README.md +1 -1
- package/package.json +1 -1
- package/rtc/channel-id.cjs +8 -6
- package/rtc/channel-id.d.ts +5 -4
- package/rtc/channel-id.js +8 -6
- package/rtc/channel-id.js.map +1 -1
- package/rtc/message-client.cjs +14 -7
- package/rtc/message-client.d.ts +3 -2
- package/rtc/message-client.js +14 -7
- package/rtc/message-client.js.map +1 -1
- package/rtc/message-host.cjs +14 -8
- package/rtc/message-host.d.ts +3 -3
- package/rtc/message-host.js +14 -8
- package/rtc/message-host.js.map +1 -1
- package/rtc/message-service.cjs +4 -4
- package/rtc/message-service.d.ts +2 -2
- package/rtc/message-service.js +4 -4
- package/rtc/message-service.js.map +1 -1
package/README.md
CHANGED
|
@@ -649,7 +649,7 @@ constructor(connection?: RTCConnectionArg, channelName?: string)
|
|
|
649
649
|
- `Promise<RTCPeerConnection>`: Async connection
|
|
650
650
|
- `() => RTCPeerConnection | Promise<RTCPeerConnection>`: Factory function
|
|
651
651
|
- Omit to initialize later via `initialize()`
|
|
652
|
-
- **`channelName`** (optional): Name for the negotiated data channel. Default: `'MessengerRTCChannelDefault'`. A deterministic
|
|
652
|
+
- **`channelName`** (optional): Name for the negotiated data channel. Default: `'MessengerRTCChannelDefault'`. A deterministic channel ID in [0, 1023] is derived from this name via FNV-1a hash.
|
|
653
653
|
|
|
654
654
|
**Examples:**
|
|
655
655
|
```typescript
|
package/package.json
CHANGED
package/rtc/channel-id.cjs
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.channelId = channelId;
|
|
4
|
+
const DEFAULT_MAX_CHANNELS = 1024;
|
|
4
5
|
/**
|
|
5
|
-
* Generates a deterministic
|
|
6
|
+
* Generates a deterministic integer in [0, 1023] from a channel name string.
|
|
6
7
|
* Used as the negotiated data channel ID for RTCDataChannel.
|
|
7
8
|
*
|
|
8
|
-
* Uses FNV-1a hash algorithm truncated to
|
|
9
|
+
* Uses FNV-1a hash algorithm truncated to 10 bits.
|
|
9
10
|
*
|
|
10
11
|
* @param name - The channel name to hash
|
|
11
|
-
* @
|
|
12
|
+
* @param max - Optional maximum value for the channel ID. Defaults to 1024.
|
|
13
|
+
* @returns A deterministic channel ID in [0, max-1]
|
|
12
14
|
*/
|
|
13
|
-
function channelId(name) {
|
|
15
|
+
function channelId(name, max = DEFAULT_MAX_CHANNELS) {
|
|
14
16
|
let hash = 0x811c9dc5; // FNV offset basis (32-bit)
|
|
15
17
|
for (let i = 0; i < name.length; i++) {
|
|
16
18
|
hash ^= name.charCodeAt(i);
|
|
17
19
|
hash = (hash * 0x01000193) >>> 0; // FNV prime, keep as unsigned 32-bit
|
|
18
20
|
}
|
|
19
|
-
// Truncate to
|
|
20
|
-
return hash %
|
|
21
|
+
// Truncate to specified range [0, max-1]
|
|
22
|
+
return hash % (max ?? DEFAULT_MAX_CHANNELS);
|
|
21
23
|
}
|
package/rtc/channel-id.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Generates a deterministic
|
|
2
|
+
* Generates a deterministic integer in [0, 1023] from a channel name string.
|
|
3
3
|
* Used as the negotiated data channel ID for RTCDataChannel.
|
|
4
4
|
*
|
|
5
|
-
* Uses FNV-1a hash algorithm truncated to
|
|
5
|
+
* Uses FNV-1a hash algorithm truncated to 10 bits.
|
|
6
6
|
*
|
|
7
7
|
* @param name - The channel name to hash
|
|
8
|
-
* @
|
|
8
|
+
* @param max - Optional maximum value for the channel ID. Defaults to 1024.
|
|
9
|
+
* @returns A deterministic channel ID in [0, max-1]
|
|
9
10
|
*/
|
|
10
|
-
export declare function channelId(name: string): number;
|
|
11
|
+
export declare function channelId(name: string, max?: number | null): number;
|
package/rtc/channel-id.js
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
+
const DEFAULT_MAX_CHANNELS = 1024;
|
|
1
2
|
/**
|
|
2
|
-
* Generates a deterministic
|
|
3
|
+
* Generates a deterministic integer in [0, 1023] from a channel name string.
|
|
3
4
|
* Used as the negotiated data channel ID for RTCDataChannel.
|
|
4
5
|
*
|
|
5
|
-
* Uses FNV-1a hash algorithm truncated to
|
|
6
|
+
* Uses FNV-1a hash algorithm truncated to 10 bits.
|
|
6
7
|
*
|
|
7
8
|
* @param name - The channel name to hash
|
|
8
|
-
* @
|
|
9
|
+
* @param max - Optional maximum value for the channel ID. Defaults to 1024.
|
|
10
|
+
* @returns A deterministic channel ID in [0, max-1]
|
|
9
11
|
*/
|
|
10
|
-
export function channelId(name) {
|
|
12
|
+
export function channelId(name, max = DEFAULT_MAX_CHANNELS) {
|
|
11
13
|
let hash = 0x811c9dc5; // FNV offset basis (32-bit)
|
|
12
14
|
for (let i = 0; i < name.length; i++) {
|
|
13
15
|
hash ^= name.charCodeAt(i);
|
|
14
16
|
hash = (hash * 0x01000193) >>> 0; // FNV prime, keep as unsigned 32-bit
|
|
15
17
|
}
|
|
16
|
-
// Truncate to
|
|
17
|
-
return hash %
|
|
18
|
+
// Truncate to specified range [0, max-1]
|
|
19
|
+
return hash % (max ?? DEFAULT_MAX_CHANNELS);
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
//# sourceMappingURL=channel-id.js.map
|
package/rtc/channel-id.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/channel-id.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/channel-id.ts"],"names":[],"mappings":"AAAA,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,MAAqB,oBAAoB;IAC/E,IAAI,IAAI,GAAG,UAAU,CAAC,CAAC,4BAA4B;IAEnD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,IAAI,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC3B,IAAI,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,qCAAqC;IACzE,CAAC;IAED,yCAAyC;IACzC,OAAO,IAAI,GAAG,CAAC,GAAG,IAAI,oBAAoB,CAAC,CAAC;AAC9C,CAAC","file":"channel-id.js","sourcesContent":["const DEFAULT_MAX_CHANNELS = 1024;\r\n\r\n/**\r\n * Generates a deterministic integer in [0, 1023] from a channel name string.\r\n * Used as the negotiated data channel ID for RTCDataChannel.\r\n *\r\n * Uses FNV-1a hash algorithm truncated to 10 bits.\r\n *\r\n * @param name - The channel name to hash\r\n * @param max - Optional maximum value for the channel ID. Defaults to 1024.\r\n * @returns A deterministic channel ID in [0, max-1]\r\n */\r\nexport function channelId(name: string, max: number | null = DEFAULT_MAX_CHANNELS): number {\r\n let hash = 0x811c9dc5; // FNV offset basis (32-bit)\r\n\r\n for (let i = 0; i < name.length; i++) {\r\n hash ^= name.charCodeAt(i);\r\n hash = (hash * 0x01000193) >>> 0; // FNV prime, keep as unsigned 32-bit\r\n }\r\n\r\n // Truncate to specified range [0, max-1]\r\n return hash % (max ?? DEFAULT_MAX_CHANNELS);\r\n}\r\n"]}
|
package/rtc/message-client.cjs
CHANGED
|
@@ -11,6 +11,7 @@ const selectors_1 = require("../selectors");
|
|
|
11
11
|
const default_channel_name_1 = require("./default-channel-name");
|
|
12
12
|
const channel_id_1 = require("./channel-id");
|
|
13
13
|
const CONNECTION = Symbol('RTCMessageClient Connection');
|
|
14
|
+
const CHANNEL_NAME = Symbol('RTCMessageClient Channel Name');
|
|
14
15
|
/**
|
|
15
16
|
* Message client for WebRTC DataChannel communication.
|
|
16
17
|
*
|
|
@@ -32,17 +33,17 @@ const CONNECTION = Symbol('RTCMessageClient Connection');
|
|
|
32
33
|
* ```
|
|
33
34
|
*/
|
|
34
35
|
class RTCMessageClient extends message_client_1.MessageClient {
|
|
35
|
-
channelName;
|
|
36
36
|
[selectors_1.RESPONSES$] = new rxjs_1.Subject();
|
|
37
37
|
[CONNECTION] = Promise.resolve([undefined, noop_1.noop]);
|
|
38
|
+
[CHANNEL_NAME];
|
|
38
39
|
/**
|
|
39
40
|
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
40
41
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
41
42
|
*/
|
|
42
43
|
constructor(connection, channelName = default_channel_name_1.DEFAULT_CHANNEL_NAME) {
|
|
43
44
|
super();
|
|
44
|
-
this
|
|
45
|
-
RTCMessageClient.prototype.initialize.call(this, connection
|
|
45
|
+
this[CHANNEL_NAME] = channelName;
|
|
46
|
+
RTCMessageClient.prototype.initialize.call(this, connection);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
48
49
|
* (Re)initializes the data channel with a new connection.
|
|
@@ -51,8 +52,7 @@ class RTCMessageClient extends message_client_1.MessageClient {
|
|
|
51
52
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
52
53
|
* @param channelName - Optional new channel name. Defaults to the current channel name.
|
|
53
54
|
*/
|
|
54
|
-
initialize(connection
|
|
55
|
-
this.channelName = channelName;
|
|
55
|
+
initialize(connection) {
|
|
56
56
|
this[CONNECTION] = Promise.race([
|
|
57
57
|
this[CONNECTION],
|
|
58
58
|
(0, timeout_1.timeout)(5000, [null, noop_1.noop])
|
|
@@ -65,9 +65,16 @@ class RTCMessageClient extends message_client_1.MessageClient {
|
|
|
65
65
|
})
|
|
66
66
|
.then(conn => {
|
|
67
67
|
if (!conn)
|
|
68
|
+
return;
|
|
69
|
+
const id = (0, channel_id_1.channelId)(this[CHANNEL_NAME], conn.sctp?.maxChannels);
|
|
70
|
+
const dataChannel = conn.createDataChannel(this[CHANNEL_NAME], { negotiated: true, id, ordered: false });
|
|
71
|
+
return new Promise((resolve) => {
|
|
72
|
+
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
73
|
+
});
|
|
74
|
+
})
|
|
75
|
+
.then(dataChannel => {
|
|
76
|
+
if (!dataChannel)
|
|
68
77
|
return [undefined, noop_1.noop];
|
|
69
|
-
const id = (0, channel_id_1.channelId)(channelName);
|
|
70
|
-
const dataChannel = conn.createDataChannel(channelName, { negotiated: true, id });
|
|
71
78
|
const listener = (event) => {
|
|
72
79
|
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
73
80
|
this[selectors_1.RESPONSES$].next(data);
|
package/rtc/message-client.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { Message } from '../message.interface';
|
|
|
5
5
|
import { GET_NEW_ID, RESPONSES$, SEND } from '../selectors';
|
|
6
6
|
import { RTCConnectionArg } from './rtc.type';
|
|
7
7
|
declare const CONNECTION: unique symbol;
|
|
8
|
+
declare const CHANNEL_NAME: unique symbol;
|
|
8
9
|
/**
|
|
9
10
|
* Message client for WebRTC DataChannel communication.
|
|
10
11
|
*
|
|
@@ -26,9 +27,9 @@ declare const CONNECTION: unique symbol;
|
|
|
26
27
|
* ```
|
|
27
28
|
*/
|
|
28
29
|
export declare class RTCMessageClient extends MessageClient {
|
|
29
|
-
private channelName;
|
|
30
30
|
[RESPONSES$]: Subject<MessageResponse>;
|
|
31
31
|
private [CONNECTION];
|
|
32
|
+
private [CHANNEL_NAME];
|
|
32
33
|
/**
|
|
33
34
|
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
34
35
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
@@ -41,7 +42,7 @@ export declare class RTCMessageClient extends MessageClient {
|
|
|
41
42
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
42
43
|
* @param channelName - Optional new channel name. Defaults to the current channel name.
|
|
43
44
|
*/
|
|
44
|
-
initialize(connection?: RTCConnectionArg
|
|
45
|
+
initialize(connection?: RTCConnectionArg): void;
|
|
45
46
|
[SEND]<T>(message: Message<T>): Promise<void>;
|
|
46
47
|
protected [GET_NEW_ID](): string;
|
|
47
48
|
}
|
package/rtc/message-client.js
CHANGED
|
@@ -8,6 +8,7 @@ import { GET_NEW_ID, RESPONSES$, SEND } from '../selectors';
|
|
|
8
8
|
import { DEFAULT_CHANNEL_NAME } from './default-channel-name';
|
|
9
9
|
import { channelId } from './channel-id';
|
|
10
10
|
const CONNECTION = Symbol('RTCMessageClient Connection');
|
|
11
|
+
const CHANNEL_NAME = Symbol('RTCMessageClient Channel Name');
|
|
11
12
|
/**
|
|
12
13
|
* Message client for WebRTC DataChannel communication.
|
|
13
14
|
*
|
|
@@ -29,17 +30,17 @@ const CONNECTION = Symbol('RTCMessageClient Connection');
|
|
|
29
30
|
* ```
|
|
30
31
|
*/
|
|
31
32
|
export class RTCMessageClient extends MessageClient {
|
|
32
|
-
channelName;
|
|
33
33
|
[RESPONSES$] = new Subject();
|
|
34
34
|
[CONNECTION] = Promise.resolve([undefined, noop]);
|
|
35
|
+
[CHANNEL_NAME];
|
|
35
36
|
/**
|
|
36
37
|
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
37
38
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
38
39
|
*/
|
|
39
40
|
constructor(connection, channelName = DEFAULT_CHANNEL_NAME) {
|
|
40
41
|
super();
|
|
41
|
-
this
|
|
42
|
-
RTCMessageClient.prototype.initialize.call(this, connection
|
|
42
|
+
this[CHANNEL_NAME] = channelName;
|
|
43
|
+
RTCMessageClient.prototype.initialize.call(this, connection);
|
|
43
44
|
}
|
|
44
45
|
/**
|
|
45
46
|
* (Re)initializes the data channel with a new connection.
|
|
@@ -48,8 +49,7 @@ export class RTCMessageClient extends MessageClient {
|
|
|
48
49
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
49
50
|
* @param channelName - Optional new channel name. Defaults to the current channel name.
|
|
50
51
|
*/
|
|
51
|
-
initialize(connection
|
|
52
|
-
this.channelName = channelName;
|
|
52
|
+
initialize(connection) {
|
|
53
53
|
this[CONNECTION] = Promise.race([
|
|
54
54
|
this[CONNECTION],
|
|
55
55
|
timeout(5000, [null, noop])
|
|
@@ -62,9 +62,16 @@ export class RTCMessageClient extends MessageClient {
|
|
|
62
62
|
})
|
|
63
63
|
.then(conn => {
|
|
64
64
|
if (!conn)
|
|
65
|
+
return;
|
|
66
|
+
const id = channelId(this[CHANNEL_NAME], conn.sctp?.maxChannels);
|
|
67
|
+
const dataChannel = conn.createDataChannel(this[CHANNEL_NAME], { negotiated: true, id, ordered: false });
|
|
68
|
+
return new Promise((resolve) => {
|
|
69
|
+
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
70
|
+
});
|
|
71
|
+
})
|
|
72
|
+
.then(dataChannel => {
|
|
73
|
+
if (!dataChannel)
|
|
65
74
|
return [undefined, noop];
|
|
66
|
-
const id = channelId(channelName);
|
|
67
|
-
const dataChannel = conn.createDataChannel(channelName, { negotiated: true, id });
|
|
68
75
|
const listener = (event) => {
|
|
69
76
|
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
70
77
|
this[RESPONSES$].next(data);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,uCAAuC,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,qCAAqC,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAG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AC,CAAC;QACL,CAAC,CAAC;aACD,IAAI,CAAC,WAAW,CAAC,EAAE;YAClB,IAAI,CAAC,WAAW;gBAAE,OAAO,CAAC,SAAS,EAAE,IAAI,CAAU,CAAC;YAEpD,MAAM,QAAQ,GAAG,CAAC,KAAmB,EAAE,EAAE;gBACvC,MAAM,IAAI,GAAoB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;gBAEnG,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC9B,CAAC,CAAC;YAEF,WAAW,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;YAElD,OAAO,CAAC,WAAW,EAAE,GAAG,EAAE;oBACxB,WAAW,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;oBACrD,WAAW,CAAC,KAAK,EAAE,CAAC;gBACtB,CAAC,CAAU,CAAC;QACd,CAAC,CAAC,CAAC;IACP,CAAC;IAEM,KAAK,CAAC,CAAC,IAAI,CAAC,CAAI,OAAmB;QACxC,MAAM,CAAC,WAAW,CAAC,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC;QAE7C,IAAI,WAAW,EAAE,UAAU,KAAK,MAAM;YAAE,OAAO;QAE/C,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAC5C,CAAC;IAES,CAAC,UAAU,CAAC;QACpB,OAAO,QAAQ,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;CACF","file":"message-client.js","sourcesContent":["import { uniqueId } from '@telperion/js-utils/unique-id';\r\nimport { noop } from '@telperion/js-utils/function/noop';\r\nimport { promisify } from '@telperion/js-utils/promise/promisify';\r\nimport { timeout } from '@telperion/js-utils/promise/timeout';\r\nimport { Subject } from 'rxjs';\r\n\r\nimport { MessageClient } from '../message-client';\r\nimport { MessageResponse } from '../message-response.type';\r\nimport { Message } from '../message.interface';\r\nimport { GET_NEW_ID, RESPONSES$, SEND } from '../selectors';\r\nimport { RTCConnectionArg } from './rtc.type';\r\nimport { DEFAULT_CHANNEL_NAME } from './default-channel-name';\r\nimport { channelId } from './channel-id';\r\n\r\nconst CONNECTION = Symbol('RTCMessageClient Connection');\r\nconst CHANNEL_NAME = Symbol('RTCMessageClient Channel Name');\r\n\r\n/**\r\n * Message client for WebRTC DataChannel communication.\r\n *\r\n * Sends request messages over a negotiated RTCDataChannel and receives responses\r\n * on the same channel. Both peers must create the channel with identical\r\n * name and negotiated ID (handled automatically via {@link channelId}).\r\n *\r\n * @example\r\n * ```typescript\r\n * class GameClient extends RTCMessageClient {\r\n * @Request('move')\r\n * sendMove(position: [number, number]): Observable<boolean> {\r\n * return null!;\r\n * }\r\n * }\r\n *\r\n * const client = new GameClient(peerConnection, 'game-events');\r\n * client.sendMove([3, 4]).subscribe(ok => console.log('Accepted:', ok));\r\n * ```\r\n */\r\nexport class RTCMessageClient extends MessageClient {\r\n public [RESPONSES$] = new Subject<MessageResponse>();\r\n private [CONNECTION] = Promise.resolve([undefined as RTCDataChannel | undefined, noop] as const);\r\n private [CHANNEL_NAME]: string;\r\n\r\n /**\r\n * @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.\r\n * @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.\r\n */\r\n constructor(\r\n connection?: RTCConnectionArg,\r\n channelName = DEFAULT_CHANNEL_NAME\r\n ) {\r\n super();\r\n\r\n this[CHANNEL_NAME] = channelName;\r\n RTCMessageClient.prototype.initialize.call(this, connection);\r\n }\r\n\r\n /**\r\n * (Re)initializes the data channel with a new connection.\r\n * Cleans up the previous channel before establishing a new one.\r\n *\r\n * @param connection - RTCPeerConnection instance, promise, or factory function.\r\n * @param channelName - Optional new channel name. Defaults to the current channel name.\r\n */\r\n initialize(connection?: RTCConnectionArg): void {\r\n this[CONNECTION] = Promise.race([\r\n this[CONNECTION],\r\n timeout(5000, [null, noop] as const)\r\n ])\r\n .then(([_, deinitialize]) => deinitialize())\r\n .catch(noop)\r\n .then(() => {\r\n const _conn = typeof connection === 'function' ? connection() : connection;\r\n\r\n return promisify(_conn);\r\n })\r\n .then(conn => {\r\n if (!conn) return;\r\n\r\n const id = channelId(this[CHANNEL_NAME], conn.sctp?.maxChannels);\r\n const dataChannel = conn.createDataChannel(this[CHANNEL_NAME], { negotiated: true, id, ordered: false });\r\n\r\n return new Promise<RTCDataChannel>((resolve) => {\r\n dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });\r\n });\r\n })\r\n .then(dataChannel => {\r\n if (!dataChannel) return [undefined, noop] as const;\r\n\r\n const listener = (event: MessageEvent) => {\r\n const data: MessageResponse = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;\r\n\r\n this[RESPONSES$].next(data);\r\n };\r\n\r\n dataChannel.addEventListener('message', listener);\r\n\r\n return [dataChannel, () => {\r\n dataChannel.removeEventListener('message', listener);\r\n dataChannel.close();\r\n }] as const;\r\n });\r\n }\r\n\r\n public async [SEND]<T>(message: Message<T>) {\r\n const [dataChannel] = await this[CONNECTION];\r\n\r\n if (dataChannel?.readyState !== 'open') return;\r\n\r\n dataChannel.send(JSON.stringify(message));\r\n }\r\n\r\n protected [GET_NEW_ID](): string {\r\n return uniqueId('messenger-rtc-message');\r\n }\r\n}\r\n"]}
|
package/rtc/message-host.cjs
CHANGED
|
@@ -11,6 +11,7 @@ const default_channel_name_1 = require("./default-channel-name");
|
|
|
11
11
|
const channel_id_1 = require("./channel-id");
|
|
12
12
|
const CONNECTION = Symbol('RTCMessageHost Connection');
|
|
13
13
|
const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
14
|
+
const CHANNEL_NAME = Symbol('RTCMessageHost Channel Name');
|
|
14
15
|
/**
|
|
15
16
|
* Message host for WebRTC DataChannel communication.
|
|
16
17
|
*
|
|
@@ -31,17 +32,17 @@ const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
|
31
32
|
* ```
|
|
32
33
|
*/
|
|
33
34
|
class RTCMessageHost extends message_host_1.MessageHost {
|
|
34
|
-
channelName;
|
|
35
35
|
[REQUESTS] = new rxjs_1.Subject();
|
|
36
36
|
[CONNECTION] = Promise.resolve([undefined, noop_1.noop]);
|
|
37
|
+
[CHANNEL_NAME];
|
|
37
38
|
/**
|
|
38
39
|
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
39
40
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
40
41
|
*/
|
|
41
42
|
constructor(connection, channelName = default_channel_name_1.DEFAULT_CHANNEL_NAME) {
|
|
42
43
|
super();
|
|
43
|
-
this
|
|
44
|
-
RTCMessageHost.prototype.initialize.call(this, connection
|
|
44
|
+
this[CHANNEL_NAME] = channelName;
|
|
45
|
+
RTCMessageHost.prototype.initialize.call(this, connection);
|
|
45
46
|
this[selectors_1.LISTEN](this[REQUESTS]);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
@@ -49,10 +50,8 @@ class RTCMessageHost extends message_host_1.MessageHost {
|
|
|
49
50
|
* Cleans up the previous channel before establishing a new one.
|
|
50
51
|
*
|
|
51
52
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
52
|
-
* @param channelName - Optional new channel name. Defaults to the current channel name.
|
|
53
53
|
*/
|
|
54
|
-
initialize(connection
|
|
55
|
-
this.channelName = channelName;
|
|
54
|
+
initialize(connection) {
|
|
56
55
|
this[CONNECTION] = Promise.race([
|
|
57
56
|
this[CONNECTION],
|
|
58
57
|
(0, timeout_1.timeout)(5000, [null, noop_1.noop])
|
|
@@ -65,9 +64,16 @@ class RTCMessageHost extends message_host_1.MessageHost {
|
|
|
65
64
|
})
|
|
66
65
|
.then(conn => {
|
|
67
66
|
if (!conn)
|
|
67
|
+
return;
|
|
68
|
+
const id = (0, channel_id_1.channelId)(this[CHANNEL_NAME], conn.sctp?.maxChannels);
|
|
69
|
+
const dataChannel = conn.createDataChannel(this[CHANNEL_NAME], { negotiated: true, id, ordered: false });
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
72
|
+
});
|
|
73
|
+
})
|
|
74
|
+
.then(dataChannel => {
|
|
75
|
+
if (!dataChannel)
|
|
68
76
|
return [undefined, noop_1.noop];
|
|
69
|
-
const id = (0, channel_id_1.channelId)(channelName);
|
|
70
|
-
const dataChannel = conn.createDataChannel(channelName, { negotiated: true, id });
|
|
71
77
|
const handler = (event) => {
|
|
72
78
|
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
73
79
|
this[REQUESTS].next(data);
|
package/rtc/message-host.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { RESPONSE } from '../selectors';
|
|
|
4
4
|
import { RTCConnectionArg } from './rtc.type';
|
|
5
5
|
declare const CONNECTION: unique symbol;
|
|
6
6
|
declare const REQUESTS: unique symbol;
|
|
7
|
+
declare const CHANNEL_NAME: unique symbol;
|
|
7
8
|
/**
|
|
8
9
|
* Message host for WebRTC DataChannel communication.
|
|
9
10
|
*
|
|
@@ -24,9 +25,9 @@ declare const REQUESTS: unique symbol;
|
|
|
24
25
|
* ```
|
|
25
26
|
*/
|
|
26
27
|
export declare class RTCMessageHost extends MessageHost {
|
|
27
|
-
private channelName;
|
|
28
28
|
private [REQUESTS];
|
|
29
29
|
private [CONNECTION];
|
|
30
|
+
private [CHANNEL_NAME];
|
|
30
31
|
/**
|
|
31
32
|
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
32
33
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
@@ -37,9 +38,8 @@ export declare class RTCMessageHost extends MessageHost {
|
|
|
37
38
|
* Cleans up the previous channel before establishing a new one.
|
|
38
39
|
*
|
|
39
40
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
40
|
-
* @param channelName - Optional new channel name. Defaults to the current channel name.
|
|
41
41
|
*/
|
|
42
|
-
initialize(connection: RTCConnectionArg
|
|
42
|
+
initialize(connection: RTCConnectionArg): void;
|
|
43
43
|
/** Closes the data channel and removes event listeners. */
|
|
44
44
|
terminate(): void;
|
|
45
45
|
protected [RESPONSE](message: MessageResponse): Promise<void>;
|
package/rtc/message-host.js
CHANGED
|
@@ -8,6 +8,7 @@ import { DEFAULT_CHANNEL_NAME } from './default-channel-name';
|
|
|
8
8
|
import { channelId } from './channel-id';
|
|
9
9
|
const CONNECTION = Symbol('RTCMessageHost Connection');
|
|
10
10
|
const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
11
|
+
const CHANNEL_NAME = Symbol('RTCMessageHost Channel Name');
|
|
11
12
|
/**
|
|
12
13
|
* Message host for WebRTC DataChannel communication.
|
|
13
14
|
*
|
|
@@ -28,17 +29,17 @@ const REQUESTS = Symbol('RTCMessageHost Requests');
|
|
|
28
29
|
* ```
|
|
29
30
|
*/
|
|
30
31
|
export class RTCMessageHost extends MessageHost {
|
|
31
|
-
channelName;
|
|
32
32
|
[REQUESTS] = new Subject();
|
|
33
33
|
[CONNECTION] = Promise.resolve([undefined, noop]);
|
|
34
|
+
[CHANNEL_NAME];
|
|
34
35
|
/**
|
|
35
36
|
* @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.
|
|
36
37
|
* @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.
|
|
37
38
|
*/
|
|
38
39
|
constructor(connection, channelName = DEFAULT_CHANNEL_NAME) {
|
|
39
40
|
super();
|
|
40
|
-
this
|
|
41
|
-
RTCMessageHost.prototype.initialize.call(this, connection
|
|
41
|
+
this[CHANNEL_NAME] = channelName;
|
|
42
|
+
RTCMessageHost.prototype.initialize.call(this, connection);
|
|
42
43
|
this[LISTEN](this[REQUESTS]);
|
|
43
44
|
}
|
|
44
45
|
/**
|
|
@@ -46,10 +47,8 @@ export class RTCMessageHost extends MessageHost {
|
|
|
46
47
|
* Cleans up the previous channel before establishing a new one.
|
|
47
48
|
*
|
|
48
49
|
* @param connection - RTCPeerConnection instance, promise, or factory function.
|
|
49
|
-
* @param channelName - Optional new channel name. Defaults to the current channel name.
|
|
50
50
|
*/
|
|
51
|
-
initialize(connection
|
|
52
|
-
this.channelName = channelName;
|
|
51
|
+
initialize(connection) {
|
|
53
52
|
this[CONNECTION] = Promise.race([
|
|
54
53
|
this[CONNECTION],
|
|
55
54
|
timeout(5000, [null, noop])
|
|
@@ -62,9 +61,16 @@ export class RTCMessageHost extends MessageHost {
|
|
|
62
61
|
})
|
|
63
62
|
.then(conn => {
|
|
64
63
|
if (!conn)
|
|
64
|
+
return;
|
|
65
|
+
const id = channelId(this[CHANNEL_NAME], conn.sctp?.maxChannels);
|
|
66
|
+
const dataChannel = conn.createDataChannel(this[CHANNEL_NAME], { negotiated: true, id, ordered: false });
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.then(dataChannel => {
|
|
72
|
+
if (!dataChannel)
|
|
65
73
|
return [undefined, noop];
|
|
66
|
-
const id = channelId(channelName);
|
|
67
|
-
const dataChannel = conn.createDataChannel(channelName, { negotiated: true, id });
|
|
68
74
|
const handler = (event) => {
|
|
69
75
|
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
70
76
|
this[REQUESTS].next(data);
|
package/rtc/message-host.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,uCAAuC,CAAC;AAClE,OAAO,EAAE,OAAO,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,IAAI,CAAC,UAAU,CAAC,CAAC;QAE7C,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7C,CAAC;CACF","file":"message-host.js","sourcesContent":["import { noop } from '@telperion/js-utils/function/noop';\r\nimport { promisify } from '@telperion/js-utils/promise/promisify';\r\nimport { timeout } from '@telperion/js-utils/promise/timeout';\r\nimport { Subject } from 'rxjs';\r\n\r\nimport { MessageHost } from '../message-host';\r\nimport { MessageResponse } from '../message-response.type';\r\nimport { Message } from '../message.interface';\r\nimport { LISTEN, RESPONSE } from '../selectors';\r\nimport { RTCConnectionArg } from './rtc.type';\r\nimport { DEFAULT_CHANNEL_NAME } from './default-channel-name';\r\nimport { channelId } from './channel-id';\r\n\r\nconst CONNECTION = Symbol('RTCMessageHost Connection');\r\nconst REQUESTS = Symbol('RTCMessageHost Requests');\r\nconst CHANNEL_NAME = Symbol('RTCMessageHost Channel Name');\r\n\r\n/**\r\n * Message host for WebRTC DataChannel communication.\r\n *\r\n * Listens for incoming messages on a negotiated RTCDataChannel and sends responses\r\n * back over the same channel. Both peers must create the channel with identical\r\n * name and negotiated ID (handled automatically via {@link channelId}).\r\n *\r\n * @example\r\n * ```typescript\r\n * class GameHost extends RTCMessageHost {\r\n * @Listen('move')\r\n * handleMove({ data }: { data: [number, number] }): Observable<boolean> {\r\n * return of(applyMove(data));\r\n * }\r\n * }\r\n *\r\n * const host = new GameHost(peerConnection, 'game-events');\r\n * ```\r\n */\r\nexport class RTCMessageHost extends MessageHost {\r\n private [REQUESTS] = new Subject<Message>();\r\n private [CONNECTION] = Promise.resolve([undefined as RTCDataChannel | undefined, noop] as const);\r\n private [CHANNEL_NAME]: string;\r\n\r\n /**\r\n * @param connection - RTCPeerConnection instance, promise, or factory function. Omit to initialize later via {@link initialize}.\r\n * @param channelName - Name for the negotiated data channel. Determines the channel ID via FNV-1a hash.\r\n */\r\n constructor(\r\n connection?: RTCConnectionArg,\r\n channelName = DEFAULT_CHANNEL_NAME\r\n ) {\r\n super();\r\n\r\n this[CHANNEL_NAME] = channelName;\r\n RTCMessageHost.prototype.initialize.call(this, connection);\r\n this[LISTEN](this[REQUESTS]);\r\n }\r\n\r\n /**\r\n * (Re)initializes the data channel with a new connection.\r\n * Cleans up the previous channel before establishing a new one.\r\n *\r\n * @param connection - RTCPeerConnection instance, promise, or factory function.\r\n */\r\n initialize(connection: RTCConnectionArg): void {\r\n this[CONNECTION] = Promise.race([\r\n this[CONNECTION],\r\n timeout(5000, [null, noop] as const)\r\n ])\r\n .then(([_, deinitialize]) => deinitialize())\r\n .catch(noop)\r\n .then(() => {\r\n const _conn = typeof connection === 'function' ? connection() : connection;\r\n\r\n return promisify(_conn);\r\n })\r\n .then(conn => {\r\n if (!conn) return;\r\n\r\n const id = channelId(this[CHANNEL_NAME], conn.sctp?.maxChannels);\r\n const dataChannel = conn.createDataChannel(this[CHANNEL_NAME], { negotiated: true, id, ordered: false });\r\n\r\n return new Promise<RTCDataChannel>((resolve) => {\r\n dataChannel.addEventListener('open', () => resolve(dataChannel), { once: true });\r\n });\r\n })\r\n .then(dataChannel => {\r\n if (!dataChannel) return [undefined, noop] as const;\r\n\r\n const handler = (event: MessageEvent) => {\r\n const data: Message = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;\r\n\r\n this[REQUESTS].next(data);\r\n };\r\n\r\n dataChannel.addEventListener('message', handler);\r\n\r\n return [dataChannel, () => {\r\n dataChannel.removeEventListener('message', handler);\r\n dataChannel.close();\r\n }] as const;\r\n });\r\n }\r\n\r\n /** Closes the data channel and removes event listeners. */\r\n public terminate(): void {\r\n this[CONNECTION]\r\n .then(([_, deinitialize]) => deinitialize())\r\n .catch(noop);\r\n }\r\n\r\n protected async [RESPONSE](message: MessageResponse) {\r\n const [dataChannel] = await this[CONNECTION];\r\n\r\n dataChannel?.send(JSON.stringify(message));\r\n }\r\n}\r\n"]}
|
package/rtc/message-service.cjs
CHANGED
|
@@ -32,11 +32,11 @@ class RTCMessageService extends (0, mixin_1.mixin)(message_host_1.RTCMessageHost
|
|
|
32
32
|
* @param channelName - Name for the negotiated data channel.
|
|
33
33
|
*/
|
|
34
34
|
constructor(connection, channelName = default_channel_name_1.DEFAULT_CHANNEL_NAME) {
|
|
35
|
-
super([connection, channelName], [connection, channelName]);
|
|
35
|
+
super([connection, `${channelName}/H`], [connection, `${channelName}/C`]);
|
|
36
36
|
}
|
|
37
|
-
initialize = (connection
|
|
38
|
-
message_host_1.RTCMessageHost.prototype.initialize.call(this, connection
|
|
39
|
-
message_client_1.RTCMessageClient.prototype.initialize.call(this, connection
|
|
37
|
+
initialize = (connection) => {
|
|
38
|
+
message_host_1.RTCMessageHost.prototype.initialize.call(this, connection);
|
|
39
|
+
message_client_1.RTCMessageClient.prototype.initialize.call(this, connection);
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
exports.RTCMessageService = RTCMessageService;
|
package/rtc/message-service.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RTCMessageClient } from "./message-client";
|
|
2
2
|
import { RTCConnectionArg } from "./rtc.type";
|
|
3
3
|
declare const RTCMessageService_base: new (args_0: [connection?: RTCConnectionArg, channelName?: string | undefined], args_1: [connection?: RTCConnectionArg, channelName?: string | undefined]) => {
|
|
4
|
-
initialize: (connection?: RTCConnectionArg
|
|
4
|
+
initialize: (connection?: RTCConnectionArg) => void;
|
|
5
5
|
terminate: () => void;
|
|
6
6
|
} & RTCMessageClient;
|
|
7
7
|
/**
|
|
@@ -31,6 +31,6 @@ export declare class RTCMessageService extends RTCMessageService_base {
|
|
|
31
31
|
* @param channelName - Name for the negotiated data channel.
|
|
32
32
|
*/
|
|
33
33
|
constructor(connection?: RTCConnectionArg, channelName?: string);
|
|
34
|
-
initialize: (connection: RTCConnectionArg
|
|
34
|
+
initialize: (connection: RTCConnectionArg) => void;
|
|
35
35
|
}
|
|
36
36
|
export {};
|
package/rtc/message-service.js
CHANGED
|
@@ -29,11 +29,11 @@ export class RTCMessageService extends mixin(RTCMessageHost, RTCMessageClient) {
|
|
|
29
29
|
* @param channelName - Name for the negotiated data channel.
|
|
30
30
|
*/
|
|
31
31
|
constructor(connection, channelName = DEFAULT_CHANNEL_NAME) {
|
|
32
|
-
super([connection, channelName], [connection, channelName]);
|
|
32
|
+
super([connection, `${channelName}/H`], [connection, `${channelName}/C`]);
|
|
33
33
|
}
|
|
34
|
-
initialize = (connection
|
|
35
|
-
RTCMessageHost.prototype.initialize.call(this, connection
|
|
36
|
-
RTCMessageClient.prototype.initialize.call(this, connection
|
|
34
|
+
initialize = (connection) => {
|
|
35
|
+
RTCMessageHost.prototype.initialize.call(this, connection);
|
|
36
|
+
RTCMessageClient.prototype.initialize.call(this, connection);
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK,CAAC,cAAc,EAAE,gBAAgB,CAAC;IAC5E;;;OAGG;IACH,YAAY,UAA6B,EAAE,WAAW,GAAG,oBAAoB;QAC3E,KAAK,CAAC,CAAC,UAAU,EAAE,WAAW,CAAC,EAAE,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/messenger/src/rtc/message-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK,CAAC,cAAc,EAAE,gBAAgB,CAAC;IAC5E;;;OAGG;IACH,YAAY,UAA6B,EAAE,WAAW,GAAG,oBAAoB;QAC3E,KAAK,CAAC,CAAC,UAAU,EAAE,GAAG,WAAW,IAAI,CAAC,EAAE,CAAC,UAAU,EAAE,GAAG,WAAW,IAAI,CAAC,CAAC,CAAC;IAC5E,CAAC;IAEe,UAAU,GAAG,CAAC,UAA4B,EAAQ,EAAE;QAClE,cAAc,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QAC3D,gBAAgB,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IAC/D,CAAC,CAAC;CACH","file":"message-service.js","sourcesContent":["import { mixin } from \"@telperion/js-utils/class/mixin\";\r\nimport { RTCMessageClient } from \"./message-client\";\r\nimport { RTCMessageHost } from \"./message-host\";\r\nimport { RTCConnectionArg } from \"./rtc.type\";\r\nimport { DEFAULT_CHANNEL_NAME } from \"./default-channel-name\";\r\n\r\n/**\r\n * Bidirectional message service for WebRTC DataChannel communication.\r\n *\r\n * Combines {@link RTCMessageHost} and {@link RTCMessageClient} via mixin,\r\n * enabling both `@Listen` and `@Request` decorators on the same class.\r\n *\r\n * @example\r\n * ```typescript\r\n * class GamePeer extends RTCMessageService {\r\n * @Request('move')\r\n * sendMove(pos: [number, number]): Observable<boolean> { return null!; }\r\n *\r\n * @Listen('move')\r\n * handleMove({ data }: { data: [number, number] }): Observable<boolean> {\r\n * return of(applyMove(data));\r\n * }\r\n * }\r\n *\r\n * const peer = new GamePeer(peerConnection, 'game-events');\r\n * ```\r\n */\r\nexport class RTCMessageService extends mixin(RTCMessageHost, RTCMessageClient) {\r\n /**\r\n * @param connection - RTCPeerConnection instance, promise, or factory function.\r\n * @param channelName - Name for the negotiated data channel.\r\n */\r\n constructor(connection?: RTCConnectionArg, channelName = DEFAULT_CHANNEL_NAME) {\r\n super([connection, `${channelName}/H`], [connection, `${channelName}/C`]);\r\n }\r\n\r\n public override initialize = (connection: RTCConnectionArg): void => {\r\n RTCMessageHost.prototype.initialize.call(this, connection);\r\n RTCMessageClient.prototype.initialize.call(this, connection);\r\n };\r\n}\r\n"]}
|