@soundworks/core 4.0.0-alpha.9 → 5.0.1
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/CHANGELOG.md +102 -0
- package/README.md +14 -29
- package/package.json +24 -42
- package/src/client/Client.js +306 -256
- package/src/client/{Context.js → ClientContext.js} +63 -54
- package/src/client/ClientContextManager.js +115 -0
- package/src/client/{Plugin.js → ClientPlugin.js} +23 -19
- package/src/client/{PluginManager.js → ClientPluginManager.js} +43 -40
- package/src/client/ClientSocket.js +270 -0
- package/src/client/{StateManager.js → ClientStateManager.js} +13 -13
- package/src/client/index.js +36 -39
- package/src/common/BasePlugin.js +64 -58
- package/src/common/BasePluginManager.js +191 -142
- package/src/common/BaseStateManager.js +635 -145
- package/src/common/BatchedTransport.js +73 -0
- package/src/common/ParameterBag.js +90 -93
- package/src/common/PromiseStore.js +54 -0
- package/src/common/SharedState.js +821 -0
- package/src/common/SharedStateCollection.js +492 -0
- package/src/common/constants.js +14 -2
- package/src/common/logger.js +64 -68
- package/src/common/shared-state-types.js +82 -0
- package/src/common/sockets-utils.js +0 -64
- package/src/common/version.js +2 -0
- package/src/server/Server.js +552 -824
- package/src/server/ServerClient.js +74 -0
- package/src/server/{Context.js → ServerContext.js} +96 -77
- package/src/server/{ContextManager.js → ServerContextManager.js} +107 -87
- package/src/server/{Plugin.js → ServerPlugin.js} +30 -32
- package/src/server/ServerPluginManager.js +177 -0
- package/src/server/ServerSocket.js +256 -0
- package/src/server/ServerSockets.js +150 -0
- package/src/server/ServerStateManager.js +706 -0
- package/src/server/SharedStatePrivate.js +277 -0
- package/src/server/audit-network-latency.worker.js +19 -4
- package/src/server/{audit-schema.js → audit-state-class-description.js} +3 -2
- package/src/server/create-http-server.js +127 -0
- package/src/server/crypto.js +8 -7
- package/src/server/index.js +60 -17
- package/types/client/Client.d.ts +95 -70
- package/types/client/{Context.d.ts → ClientContext.d.ts} +59 -34
- package/types/client/{ContextManager.d.ts → ClientContextManager.d.ts} +19 -22
- package/types/client/ClientPlugin.d.ts +51 -0
- package/types/client/ClientPluginManager.d.ts +112 -0
- package/types/client/ClientSocket.d.ts +76 -0
- package/types/client/{StateManager.d.ts → ClientStateManager.d.ts} +18 -21
- package/types/client/index.d.ts +33 -6
- package/types/common/BasePlugin.d.ts +64 -53
- package/types/common/BasePluginManager.d.ts +84 -39
- package/types/common/BaseStateManager.d.ts +227 -49
- package/types/common/BatchedTransport.d.ts +17 -0
- package/types/common/ParameterBag.d.ts +73 -76
- package/types/common/PromiseStore.d.ts +11 -0
- package/types/common/SharedState.d.ts +291 -0
- package/types/common/SharedStateCollection.d.ts +311 -0
- package/types/common/constants.d.ts +10 -3
- package/types/common/logger.d.ts +8 -6
- package/types/common/shared-state-types.d.ts +95 -0
- package/types/common/sockets-utils.d.ts +0 -5
- package/types/common/version.d.ts +2 -0
- package/types/server/Server.d.ts +215 -136
- package/types/server/ServerClient.d.ts +45 -0
- package/types/server/ServerContext.d.ts +209 -0
- package/types/server/ServerContextManager.d.ts +75 -0
- package/types/server/ServerPlugin.d.ts +57 -0
- package/types/server/ServerPluginManager.d.ts +115 -0
- package/types/server/ServerSocket.d.ts +69 -0
- package/types/server/ServerSockets.d.ts +42 -0
- package/types/server/ServerStateManager.d.ts +284 -0
- package/types/server/SharedStatePrivate.d.ts +23 -0
- package/types/server/audit-network-latency.worker.d.ts +2 -2
- package/types/server/{audit-schema.d.ts → audit-state-class-description.d.ts} +8 -9
- package/types/server/create-http-server.d.ts +5 -0
- package/types/server/crypto.d.ts +4 -0
- package/types/server/index.d.ts +51 -3
- package/.eslintrc +0 -15
- package/.github/workflows/gh-pages.yml +0 -31
- package/.github/workflows/unit-tests.yml +0 -18
- package/src/client/ContextManager.js +0 -111
- package/src/client/SharedState.js +0 -84
- package/src/client/SharedStateCollection.js +0 -35
- package/src/client/Socket.js +0 -402
- package/src/common/BaseSharedState.js +0 -583
- package/src/common/BaseSharedStateCollection.js +0 -179
- package/src/common/SharedStatePrivate.js +0 -192
- package/src/common/promise-store.js +0 -25
- package/src/server/Client.js +0 -55
- package/src/server/PluginManager.js +0 -170
- package/src/server/SharedState.js +0 -85
- package/src/server/SharedStateCollection.js +0 -36
- package/src/server/Socket.js +0 -396
- package/src/server/Sockets.js +0 -224
- package/src/server/StateManager.js +0 -620
- package/tsconfig.json +0 -21
- package/types/client/Client.d.ts.map +0 -1
- package/types/client/Context.d.ts.map +0 -1
- package/types/client/ContextManager.d.ts.map +0 -1
- package/types/client/Plugin.d.ts +0 -53
- package/types/client/Plugin.d.ts.map +0 -1
- package/types/client/PluginManager.d.ts +0 -96
- package/types/client/PluginManager.d.ts.map +0 -1
- package/types/client/SharedState.d.ts +0 -85
- package/types/client/SharedState.d.ts.map +0 -1
- package/types/client/Socket.d.ts +0 -138
- package/types/client/Socket.d.ts.map +0 -1
- package/types/client/StateManager.d.ts.map +0 -1
- package/types/client/index.d.ts.map +0 -1
- package/types/common/BasePlugin.d.ts.map +0 -1
- package/types/common/BasePluginManager.d.ts.map +0 -1
- package/types/common/BaseSharedState.d.ts +0 -173
- package/types/common/BaseSharedState.d.ts.map +0 -1
- package/types/common/BaseStateManager.d.ts.map +0 -1
- package/types/common/ParameterBag.d.ts.map +0 -1
- package/types/common/SharedStatePrivate.d.ts +0 -20
- package/types/common/SharedStatePrivate.d.ts.map +0 -1
- package/types/common/constants.d.ts.map +0 -1
- package/types/common/logger.d.ts.map +0 -1
- package/types/common/promise-store.d.ts +0 -4
- package/types/common/promise-store.d.ts.map +0 -1
- package/types/common/sockets-utils.d.ts.map +0 -1
- package/types/common/utils.d.ts +0 -5
- package/types/common/utils.d.ts.map +0 -1
- package/types/server/Client.d.ts +0 -40
- package/types/server/Client.d.ts.map +0 -1
- package/types/server/Context.d.ts +0 -59
- package/types/server/Context.d.ts.map +0 -1
- package/types/server/ContextManager.d.ts +0 -69
- package/types/server/ContextManager.d.ts.map +0 -1
- package/types/server/Plugin.d.ts +0 -70
- package/types/server/Plugin.d.ts.map +0 -1
- package/types/server/PluginManager.d.ts +0 -91
- package/types/server/PluginManager.d.ts.map +0 -1
- package/types/server/Server.d.ts.map +0 -1
- package/types/server/SharedState.d.ts +0 -86
- package/types/server/SharedState.d.ts.map +0 -1
- package/types/server/Socket.d.ts +0 -128
- package/types/server/Socket.d.ts.map +0 -1
- package/types/server/Sockets.d.ts +0 -73
- package/types/server/Sockets.d.ts.map +0 -1
- package/types/server/StateManager.d.ts +0 -190
- package/types/server/StateManager.d.ts.map +0 -1
- package/types/server/audit-network-latency.worker.d.ts.map +0 -1
- package/types/server/audit-schema.d.ts.map +0 -1
- package/types/server/index.d.ts.map +0 -1
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import BaseSharedState from '../common/BaseSharedState.js';
|
|
2
|
-
|
|
3
|
-
// this file exists only for documentation purposes
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @callback server.SharedState~onUpdateCallback
|
|
7
|
-
* @param {Object} newValues - Key / value pairs of the updates that have been
|
|
8
|
-
* applied to the state.
|
|
9
|
-
* @param {Object} oldValues - Key / value pairs of the updated params before
|
|
10
|
-
* the updates has been applied to the state.
|
|
11
|
-
* @param {Mixed} [context=null] - Optionnal context object that has been passed
|
|
12
|
-
* with the values updates in the `set` call.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Delete the registered {@link server.SharedState~onUpdateCallback}.
|
|
17
|
-
*
|
|
18
|
-
* @callback server.SharedState~deleteOnUpdateCallback
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The `SharedState` is one of the most important and versatile abstraction provided
|
|
23
|
-
* by `soundworks`. It represents a set of parameters that are synchronized accross
|
|
24
|
-
* every nodes of the application (clients and server) that declared some interest
|
|
25
|
-
* to the shared state.
|
|
26
|
-
*
|
|
27
|
-
* A `SharedState` is created according to a "schema" (in the sense of a database
|
|
28
|
-
* schema) that must be declared and registered server-side. Any number of `SharedState`s
|
|
29
|
-
* can be created from a single schema.
|
|
30
|
-
*
|
|
31
|
-
* A shared state can be created both by the clients or by the server (in which case
|
|
32
|
-
* it is generally considered as a global state of the application). Similarly any
|
|
33
|
-
* node of the application (clients or server) can declare interest and "attach" to
|
|
34
|
-
* a state created by another node. All node attached to a state can modify its values
|
|
35
|
-
* and/or react to the modifications applied by other nodes.
|
|
36
|
-
*
|
|
37
|
-
* Tutorial: {@link https://soundworks.dev/guide/state-manager.html}
|
|
38
|
-
*
|
|
39
|
-
* ```
|
|
40
|
-
* // server-side
|
|
41
|
-
* import { Server } from '@soundworks/server/index.js';
|
|
42
|
-
*
|
|
43
|
-
* const server = new Server(config);
|
|
44
|
-
* // declare and register the schema of a shared state.
|
|
45
|
-
* server.stateManager.registerSchema('some-global-state', {
|
|
46
|
-
* myRandom: {
|
|
47
|
-
* type: 'float',
|
|
48
|
-
* default: 0,
|
|
49
|
-
* }
|
|
50
|
-
* });
|
|
51
|
-
*
|
|
52
|
-
* await server.start();
|
|
53
|
-
*
|
|
54
|
-
* // create a global state server-side
|
|
55
|
-
* const globalState = await server.stateManager.create('some-global-state');
|
|
56
|
-
* // listen and react to the changes made by the clients
|
|
57
|
-
* globalState.onUpdate(updates => console.log(updates));
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* ```
|
|
61
|
-
* // client-side
|
|
62
|
-
* import { Client } from '@soundworks/client.index.js';
|
|
63
|
-
*
|
|
64
|
-
* const client = new Client(config);
|
|
65
|
-
* await client.start();
|
|
66
|
-
*
|
|
67
|
-
* // attach to the global state created by the server
|
|
68
|
-
* const globalState = await client.stateManager.attach('some-global-state');
|
|
69
|
-
*
|
|
70
|
-
* // update the value of a `myRandom` parameter every seconds
|
|
71
|
-
* setInterval(() => {
|
|
72
|
-
* globalState.set({ myRandom: Math.random() });
|
|
73
|
-
* }, 1000);
|
|
74
|
-
* ``
|
|
75
|
-
*
|
|
76
|
-
* @memberof server
|
|
77
|
-
* @extends BaseSharedState
|
|
78
|
-
* @inheritdoc
|
|
79
|
-
* @hideconstructor
|
|
80
|
-
* @see {server.StateManager}
|
|
81
|
-
*/
|
|
82
|
-
class SharedState extends BaseSharedState {}
|
|
83
|
-
|
|
84
|
-
export default SharedState;
|
|
85
|
-
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import BaseSharedStateCollection from '../common/BaseSharedStateCollection.js';
|
|
2
|
-
|
|
3
|
-
// this file exists only for documentation purposesSharedStateCollection.js
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @callback server.SharedStateCollection~onUpdateCallback
|
|
7
|
-
* @param {server.SharedState} state - State that triggered the update.
|
|
8
|
-
* @param {Object} newValues - Key / value pairs of the updates that have been
|
|
9
|
-
* applied to the state.
|
|
10
|
-
* @param {Object} oldValues - Key / value pairs of the updated params before
|
|
11
|
-
* the updates has been applied to the state.
|
|
12
|
-
* @param {Mixed} [context=null] - Optionnal context object that has been passed
|
|
13
|
-
* with the values updates in the `set` call.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The `SharedStateCollection` interface represent a collection of all states
|
|
18
|
-
* created from a given schema name on the network, at the execption of the ones
|
|
19
|
-
* created by the current node.
|
|
20
|
-
*
|
|
21
|
-
* ```
|
|
22
|
-
* const collection = await server.stateManager.getCollection('my-schema');
|
|
23
|
-
* const allValues = collection.getValues();
|
|
24
|
-
* collection.onUpdate((state, newValues, oldValues, context) => {
|
|
25
|
-
* // do something
|
|
26
|
-
* });
|
|
27
|
-
* ```
|
|
28
|
-
* @memberof server
|
|
29
|
-
* @extends BaseSharedStateCollection
|
|
30
|
-
* @inheritdoc
|
|
31
|
-
* @hideconstructor
|
|
32
|
-
*/
|
|
33
|
-
class SharedStateCollection extends BaseSharedStateCollection {}
|
|
34
|
-
|
|
35
|
-
export default SharedStateCollection;
|
|
36
|
-
|
package/src/server/Socket.js
DELETED
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
import { getTime } from '@ircam/sc-gettime';
|
|
2
|
-
import {
|
|
3
|
-
packBinaryMessage,
|
|
4
|
-
unpackBinaryMessage,
|
|
5
|
-
packStringMessage,
|
|
6
|
-
unpackStringMessage,
|
|
7
|
-
} from '../common/sockets-utils.js';
|
|
8
|
-
|
|
9
|
-
// Status codes:
|
|
10
|
-
//
|
|
11
|
-
// CONNECTING = 0;
|
|
12
|
-
// OPEN = 1;
|
|
13
|
-
// CLOSING = 2;
|
|
14
|
-
// CLOSED = 3;
|
|
15
|
-
// READY_STATES = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The Socket class is a simple publish / subscribe wrapper built on top of the
|
|
19
|
-
* [ws](https://github.com/websockets/ws) library. An instance of {@link server.Socket}
|
|
20
|
-
* is automatically created per client when it connects (see {@link server.Client#socket}).
|
|
21
|
-
*
|
|
22
|
-
* _Important: In most cases, you should consider using a {@link client.SharedState}
|
|
23
|
-
* rather than directly using the sockets._
|
|
24
|
-
*
|
|
25
|
-
* The Socket class contains two different WebSockets:
|
|
26
|
-
* - a socket configured with `binaryType = 'blob'` for JSON compatible data
|
|
27
|
-
* types (i.e. string, number, boolean, object, array and null).
|
|
28
|
-
* - a socket configured with `binaryType= 'arraybuffer'` for efficient streaming
|
|
29
|
-
* of binary data.
|
|
30
|
-
*
|
|
31
|
-
* @memberof server
|
|
32
|
-
* @hideconstructor
|
|
33
|
-
*/
|
|
34
|
-
class Socket {
|
|
35
|
-
constructor(ws, binaryWs, rooms, sockets, options = {}) {
|
|
36
|
-
/**
|
|
37
|
-
* Configuration object
|
|
38
|
-
*
|
|
39
|
-
* @type {object}
|
|
40
|
-
*/
|
|
41
|
-
this.config = {
|
|
42
|
-
pingInterval: 5 * 1000,
|
|
43
|
-
...options,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* `ws` socket instance configured with `binaryType=blob` (string)
|
|
48
|
-
*
|
|
49
|
-
* @type {object}
|
|
50
|
-
* @private
|
|
51
|
-
*/
|
|
52
|
-
this.ws = ws;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
|
|
56
|
-
*
|
|
57
|
-
* @type {object}
|
|
58
|
-
* @private
|
|
59
|
-
*/
|
|
60
|
-
this.binaryWs = binaryWs;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Reference to the sockets object, is mainly dedicated to allow
|
|
64
|
-
* broadcasting from a given socket instance.
|
|
65
|
-
*
|
|
66
|
-
* @type {server.Sockets}
|
|
67
|
-
* @example
|
|
68
|
-
* socket.sockets.broadcast('my-room', this, 'update-value', 1);
|
|
69
|
-
*/
|
|
70
|
-
this.sockets = sockets;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Reference to the rooms object
|
|
74
|
-
*
|
|
75
|
-
* @type {Map}
|
|
76
|
-
* @private
|
|
77
|
-
*/
|
|
78
|
-
this.rooms = rooms;
|
|
79
|
-
|
|
80
|
-
/** @private */
|
|
81
|
-
this._stringListeners = new Map();
|
|
82
|
-
/** @private */
|
|
83
|
-
this._binaryListeners = new Map();
|
|
84
|
-
|
|
85
|
-
// ----------------------------------------------------------
|
|
86
|
-
// init string socket
|
|
87
|
-
// ----------------------------------------------------------
|
|
88
|
-
this.ws.addEventListener('message', e => {
|
|
89
|
-
const [channel, args] = unpackStringMessage(e.data);
|
|
90
|
-
this._emit(false, channel, ...args);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// broadcast all `ws` "native" events
|
|
94
|
-
[
|
|
95
|
-
'close',
|
|
96
|
-
'error',
|
|
97
|
-
'message',
|
|
98
|
-
'open',
|
|
99
|
-
'ping',
|
|
100
|
-
'pong',
|
|
101
|
-
'unexpected-response',
|
|
102
|
-
'upgrade',
|
|
103
|
-
].forEach(eventName => {
|
|
104
|
-
this.ws.addEventListener(eventName, e => {
|
|
105
|
-
this._emit(false, eventName, e.data);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// ----------------------------------------------------------
|
|
110
|
-
// init binary socket
|
|
111
|
-
// ----------------------------------------------------------
|
|
112
|
-
this.binaryWs.addEventListener('message', e => {
|
|
113
|
-
const [channel, data] = unpackBinaryMessage(e.data);
|
|
114
|
-
this._emit(true, channel, data);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
// broadcast all `ws` "native" events
|
|
118
|
-
[
|
|
119
|
-
'close',
|
|
120
|
-
'error',
|
|
121
|
-
'message',
|
|
122
|
-
'open',
|
|
123
|
-
'ping',
|
|
124
|
-
'pong',
|
|
125
|
-
'unexpected-response',
|
|
126
|
-
'upgrade',
|
|
127
|
-
].forEach(eventName => {
|
|
128
|
-
this.binaryWs.addEventListener(eventName, e => {
|
|
129
|
-
this._emit(true, eventName, e.data);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// heartbeat system (run only on string socket), adapted from:
|
|
134
|
-
// https://github.com/websockets/ws#how-to-detect-and-close-broken-connections
|
|
135
|
-
this._isAlive = true;
|
|
136
|
-
let msg = {
|
|
137
|
-
type: 'add-measurement',
|
|
138
|
-
value: {
|
|
139
|
-
ping: 0,
|
|
140
|
-
pong: 0,
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
// heartbeat system, only on "regular" socket
|
|
145
|
-
this.ws.on('pong', () => {
|
|
146
|
-
this._isAlive = true;
|
|
147
|
-
|
|
148
|
-
msg.value.pong = getTime();
|
|
149
|
-
this.sockets._latencyStatsWorker.postMessage(msg);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
this._intervalId = setInterval(() => {
|
|
153
|
-
if (this._isAlive === false) {
|
|
154
|
-
// emit a 'close' event to go trough all the disconnection pipeline
|
|
155
|
-
this._emit(false, 'close');
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
this._isAlive = false;
|
|
160
|
-
msg.value.ping = getTime();
|
|
161
|
-
|
|
162
|
-
this.ws.ping();
|
|
163
|
-
}, this.config.pingInterval);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Removes all listeners and immediately close the two sockets. Is automatically
|
|
168
|
-
* called on `server.stop()`
|
|
169
|
-
*
|
|
170
|
-
* @private
|
|
171
|
-
*/
|
|
172
|
-
terminate() {
|
|
173
|
-
// clear ping/pong check
|
|
174
|
-
clearInterval(this._intervalId);
|
|
175
|
-
// clean rooms
|
|
176
|
-
for (let [_key, room] of this.rooms) {
|
|
177
|
-
room.delete(this);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// clear references to sockets and rooms
|
|
181
|
-
this.sockets = null;
|
|
182
|
-
this.rooms = null;
|
|
183
|
-
|
|
184
|
-
// clear all listeners
|
|
185
|
-
this._stringListeners.clear();
|
|
186
|
-
this._binaryListeners.clear();
|
|
187
|
-
|
|
188
|
-
// clear "native" listeners
|
|
189
|
-
[this.binaryWs, this.ws].forEach((socket) => {
|
|
190
|
-
[
|
|
191
|
-
'close',
|
|
192
|
-
'error',
|
|
193
|
-
'message',
|
|
194
|
-
'open',
|
|
195
|
-
'ping',
|
|
196
|
-
'pong',
|
|
197
|
-
'unexpected-response',
|
|
198
|
-
'upgrade',
|
|
199
|
-
].forEach(eventName => {
|
|
200
|
-
socket.removeAllListeners(eventName);
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// clear binarySocket as this is called from the string one.
|
|
205
|
-
this.binaryWs.terminate();
|
|
206
|
-
this.ws.terminate();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* @param {boolean} binary - Emit to either the string or binary socket.
|
|
211
|
-
* @param {string} channel - Channel name.
|
|
212
|
-
* @param {...*} args - Content of the message.
|
|
213
|
-
* @private
|
|
214
|
-
*/
|
|
215
|
-
_emit(binary, channel, ...args) {
|
|
216
|
-
const listeners = binary ? this._binaryListeners : this._stringListeners;
|
|
217
|
-
|
|
218
|
-
if (listeners.has(channel)) {
|
|
219
|
-
const callbacks = listeners.get(channel);
|
|
220
|
-
callbacks.forEach(callback => callback(...args));
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @param {Function[]} listeners - List of listeners, either for the string or binary socket.
|
|
226
|
-
* @param {string} channel - Channel name.
|
|
227
|
-
* @param {Function} callback - The function to be added to the listeners.
|
|
228
|
-
* @private
|
|
229
|
-
*/
|
|
230
|
-
_addListener(listeners, channel, callback) {
|
|
231
|
-
if (!listeners.has(channel)) {
|
|
232
|
-
listeners.set(channel, new Set());
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const callbacks = listeners.get(channel);
|
|
236
|
-
callbacks.add(callback);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* @param {Function[]} listeners - List of listeners, either for the string or binary socket.
|
|
241
|
-
* @param {string} channel - Channel name.
|
|
242
|
-
* @param {Function} callback - The function to be removed from the listeners.
|
|
243
|
-
* @private
|
|
244
|
-
*/
|
|
245
|
-
_removeListener(listeners, channel, callback) {
|
|
246
|
-
if (listeners.has(channel)) {
|
|
247
|
-
const callbacks = listeners.get(channel);
|
|
248
|
-
callbacks.delete(callback);
|
|
249
|
-
|
|
250
|
-
if (callbacks.size === 0) {
|
|
251
|
-
listeners.delete(channel);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* @param {Function[]} listeners - List of listeners, either for the string or binary socket.
|
|
258
|
-
* @param {string} [channel=null] - Channel name of the listeners to remove. If null
|
|
259
|
-
* all the listeners are cleared.
|
|
260
|
-
* @private
|
|
261
|
-
*/
|
|
262
|
-
_removeAllListeners(listeners, channel) {
|
|
263
|
-
if (channel === null) {
|
|
264
|
-
listeners.clear();
|
|
265
|
-
} else if (listeners.has(channel)) {
|
|
266
|
-
listeners.delete(channel);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Add the socket to a room
|
|
272
|
-
*
|
|
273
|
-
* @param {string} roomId - Id of the room.
|
|
274
|
-
*/
|
|
275
|
-
addToRoom(roomId) {
|
|
276
|
-
if (!this.rooms.has(roomId)) {
|
|
277
|
-
this.rooms.set(roomId, new Set());
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const room = this.rooms.get(roomId);
|
|
281
|
-
room.add(this);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Remove the socket from a room
|
|
286
|
-
*
|
|
287
|
-
* @param {string} roomId - Id of the room.
|
|
288
|
-
*/
|
|
289
|
-
removeFromRoom(roomId) {
|
|
290
|
-
if (this.rooms.has(roomId)) {
|
|
291
|
-
const room = this.rooms.get(roomId);
|
|
292
|
-
room.delete(this);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Send messages with JSON compatible data types on a given channel.
|
|
298
|
-
*
|
|
299
|
-
* @param {string} channel - Channel name.
|
|
300
|
-
* @param {...*} args - Payload of the message. As many arguments as needed, of
|
|
301
|
-
* JSON compatible data types (i.e. string, number, boolean, object, array and null).
|
|
302
|
-
*/
|
|
303
|
-
send(channel, ...args) {
|
|
304
|
-
const msg = packStringMessage(channel, ...args);
|
|
305
|
-
|
|
306
|
-
// 0 CONNECTING Socket has been created. The connection is not yet open.
|
|
307
|
-
// 1 OPEN The connection is open and ready to communicate.
|
|
308
|
-
// 2 CLOSING The connection is in the process of closing.
|
|
309
|
-
// 3 CLOSED The connection is closed or couldn't be opened.
|
|
310
|
-
if (this.ws.readyState === 1) {
|
|
311
|
-
this.ws.send(msg, (err) => {
|
|
312
|
-
if (err) {
|
|
313
|
-
console.error('error sending msg:', channel, args, err);
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Listen messages with JSON compatible data types on a given channel.
|
|
321
|
-
*
|
|
322
|
-
* @param {string} channel - Channel name.
|
|
323
|
-
* @param {Function} callback - Callback to execute when a message is received,
|
|
324
|
-
* arguments of the callback function will match the arguments sent using the
|
|
325
|
-
* {@link server.Socket#send} method.
|
|
326
|
-
*/
|
|
327
|
-
addListener(channel, callback) {
|
|
328
|
-
this._addListener(this._stringListeners, channel, callback);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Remove a listener of messages with JSON compatible data types from a given channel.
|
|
333
|
-
*
|
|
334
|
-
* @param {string} channel - Channel name.
|
|
335
|
-
* @param {Function} callback - Callback to remove.
|
|
336
|
-
*/
|
|
337
|
-
removeListener(channel, callback) {
|
|
338
|
-
this._removeListener(this._stringListeners, channel, callback);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Remove all listeners of messages with JSON compatible data types.
|
|
343
|
-
*
|
|
344
|
-
* @param {string} channel - Channel name.
|
|
345
|
-
*/
|
|
346
|
-
removeAllListeners(channel = null) {
|
|
347
|
-
this._removeAllListeners(this._stringListeners, channel);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Send binary messages on a given channel.
|
|
352
|
-
*
|
|
353
|
-
* @param {string} channel - Channel name.
|
|
354
|
-
* @param {TypedArray} typedArray - Binary data to be sent.
|
|
355
|
-
*/
|
|
356
|
-
sendBinary(channel, typedArray) {
|
|
357
|
-
const msg = packBinaryMessage(channel, typedArray);
|
|
358
|
-
|
|
359
|
-
this.binaryWs.send(msg, (err) => {
|
|
360
|
-
if (err) {
|
|
361
|
-
console.error('error sending msg:', channel, typedArray);
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Listen binary messages on a given channel
|
|
368
|
-
*
|
|
369
|
-
* @param {string} channel - Channel name.
|
|
370
|
-
* @param {Function} callback - Callback to execute when a message is received.
|
|
371
|
-
*/
|
|
372
|
-
addBinaryListener(channel, callback) {
|
|
373
|
-
this._addListener(this._binaryListeners, channel, callback);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Remove a listener of binary compatible messages from a given channel
|
|
378
|
-
*
|
|
379
|
-
* @param {string} channel - Channel name.
|
|
380
|
-
* @param {Function} callback - Callback to remove.
|
|
381
|
-
*/
|
|
382
|
-
removeBinaryListener(channel, callback) {
|
|
383
|
-
this._removeListener(this._binaryListeners, channel, callback);
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Remove all listeners of binary compatible messages on a given channel
|
|
388
|
-
*
|
|
389
|
-
* @param {string} channel - Channel of the message.
|
|
390
|
-
*/
|
|
391
|
-
removeAllBinaryListeners(channel = null) {
|
|
392
|
-
this._removeAllListeners(this._binaryListeners, channel);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
export default Socket;
|