@soundworks/core 3.6.0 → 4.0.0-alpha.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/.eslintrc +16 -0
- package/LICENSE +1 -1
- package/README.md +32 -30
- package/package.json +57 -26
- package/src/client/Client.js +391 -0
- package/src/client/Context.js +225 -0
- package/src/client/ContextManager.js +111 -0
- package/src/client/Plugin.js +66 -0
- package/src/client/PluginManager.js +123 -0
- package/src/client/SharedState.js +84 -0
- package/src/client/Socket.js +389 -0
- package/src/client/StateManager.js +67 -0
- package/src/client/index.js +77 -0
- package/src/common/BasePlugin.js +163 -0
- package/src/common/BasePluginManager.js +227 -0
- package/src/common/BaseSharedState.js +473 -0
- package/src/common/BaseStateManager.js +200 -0
- package/{common → src/common}/ParameterBag.js +82 -122
- package/{common → src/common}/SharedStatePrivate.js +56 -64
- package/src/common/constants.js +51 -0
- package/src/common/logger.js +154 -0
- package/src/common/promise-store.js +25 -0
- package/src/common/sockets-utils.js +73 -0
- package/src/common/utils.js +22 -0
- package/src/server/Client.js +50 -0
- package/src/server/Context.js +99 -0
- package/src/server/ContextManager.js +266 -0
- package/src/server/Plugin.js +77 -0
- package/src/server/PluginManager.js +150 -0
- package/src/server/Server.js +849 -0
- package/src/server/SharedState.js +86 -0
- package/{server → src/server}/Socket.js +119 -94
- package/{server → src/server}/Sockets.js +73 -61
- package/{common/SharedStateManagerServer.js → src/server/StateManager.js} +183 -131
- package/src/server/audit-network-latency.worker.js +57 -0
- package/src/server/audit-schema.js +42 -0
- package/src/server/index.js +45 -0
- package/tsconfig.json +21 -0
- package/types/client/Client.d.ts +194 -0
- package/types/client/Client.d.ts.map +1 -0
- package/types/client/Context.d.ts +139 -0
- package/types/client/Context.d.ts.map +1 -0
- package/types/client/ContextManager.d.ts +45 -0
- package/types/client/ContextManager.d.ts.map +1 -0
- package/types/client/Plugin.d.ts +53 -0
- package/types/client/Plugin.d.ts.map +1 -0
- package/types/client/PluginManager.d.ts +96 -0
- package/types/client/PluginManager.d.ts.map +1 -0
- package/types/client/SharedState.d.ts +85 -0
- package/types/client/SharedState.d.ts.map +1 -0
- package/types/client/Socket.d.ts +138 -0
- package/types/client/Socket.d.ts.map +1 -0
- package/types/client/StateManager.d.ts +72 -0
- package/types/client/StateManager.d.ts.map +1 -0
- package/types/client/index.d.ts +11 -0
- package/types/client/index.d.ts.map +1 -0
- package/types/common/BasePlugin.d.ts +145 -0
- package/types/common/BasePlugin.d.ts.map +1 -0
- package/types/common/BasePluginManager.d.ts +88 -0
- package/types/common/BasePluginManager.d.ts.map +1 -0
- package/types/common/BaseSharedState.d.ts +173 -0
- package/types/common/BaseSharedState.d.ts.map +1 -0
- package/types/common/BaseStateManager.d.ts +70 -0
- package/types/common/BaseStateManager.d.ts.map +1 -0
- package/types/common/ParameterBag.d.ts +143 -0
- package/types/common/ParameterBag.d.ts.map +1 -0
- package/types/common/SharedStatePrivate.d.ts +20 -0
- package/types/common/SharedStatePrivate.d.ts.map +1 -0
- package/types/common/constants.d.ts +35 -0
- package/types/common/constants.d.ts.map +1 -0
- package/types/common/logger.d.ts +16 -0
- package/types/common/logger.d.ts.map +1 -0
- package/types/common/promise-store.d.ts +4 -0
- package/types/common/promise-store.d.ts.map +1 -0
- package/types/common/sockets-utils.d.ts +9 -0
- package/types/common/sockets-utils.d.ts.map +1 -0
- package/types/common/utils.d.ts +5 -0
- package/types/common/utils.d.ts.map +1 -0
- package/types/server/Client.d.ts +40 -0
- package/types/server/Client.d.ts.map +1 -0
- package/types/server/Context.d.ts +59 -0
- package/types/server/Context.d.ts.map +1 -0
- package/types/server/ContextManager.d.ts +69 -0
- package/types/server/ContextManager.d.ts.map +1 -0
- package/types/server/Plugin.d.ts +70 -0
- package/types/server/Plugin.d.ts.map +1 -0
- package/types/server/PluginManager.d.ts +91 -0
- package/types/server/PluginManager.d.ts.map +1 -0
- package/types/server/Server.d.ts +223 -0
- package/types/server/Server.d.ts.map +1 -0
- package/types/server/SharedState.d.ts +86 -0
- package/types/server/SharedState.d.ts.map +1 -0
- package/types/server/Socket.d.ts +128 -0
- package/types/server/Socket.d.ts.map +1 -0
- package/types/server/Sockets.d.ts +73 -0
- package/types/server/Sockets.d.ts.map +1 -0
- package/types/server/StateManager.d.ts +190 -0
- package/types/server/StateManager.d.ts.map +1 -0
- package/types/server/audit-network-latency.worker.d.ts +2 -0
- package/types/server/audit-network-latency.worker.d.ts.map +1 -0
- package/types/server/audit-schema.d.ts +27 -0
- package/types/server/audit-schema.d.ts.map +1 -0
- package/types/server/index.d.ts +7 -0
- package/types/server/index.d.ts.map +1 -0
- package/client/AbstractExperience.js +0 -54
- package/client/AbstractPlugin.js +0 -230
- package/client/Client.js +0 -190
- package/client/PluginManager.js +0 -267
- package/client/Socket.js +0 -338
- package/client/index.js +0 -48
- package/client.js +0 -28
- package/common/SharedState.js +0 -480
- package/common/SharedStateManagerClient.js +0 -194
- package/common/Signal.js +0 -40
- package/common/SignalAll.js +0 -44
- package/common/shared-state-utils.js +0 -90
- package/common/sockets-encoder-decoder.js +0 -73
- package/server/AbstractExperience.js +0 -126
- package/server/AbstractPlugin.js +0 -211
- package/server/Client.js +0 -70
- package/server/PluginManager.js +0 -177
- package/server/Server.js +0 -746
- package/server/index.js +0 -49
- package/server/utils/logger.js +0 -134
- package/server.js +0 -28
|
@@ -0,0 +1,86 @@
|
|
|
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
|
+
/**
|
|
23
|
+
* The `SharedState` is one of the most important and versatile abstraction provided
|
|
24
|
+
* by `soundworks`. It represents a set of parameters that are synchronized accross
|
|
25
|
+
* every nodes of the application (clients and server) that declared some interest
|
|
26
|
+
* to the shared state.
|
|
27
|
+
*
|
|
28
|
+
* A `SharedState` is created according to a "schema" (in the sense of a database
|
|
29
|
+
* schema) that must be declared and registered server-side. Any number of `SharedState`s
|
|
30
|
+
* can be created from a single schema.
|
|
31
|
+
*
|
|
32
|
+
* A shared state can be created both by the clients or by the server (in which case
|
|
33
|
+
* it is generally considered as a global state of the application). Similarly any
|
|
34
|
+
* node of the application (clients or server) can declare interest and "attach" to
|
|
35
|
+
* a state created by another node. All node attached to a state can modify its values
|
|
36
|
+
* and/or react to the modifications applied by other nodes.
|
|
37
|
+
*
|
|
38
|
+
* Tutorial: {@link https://soundworks.dev/guide/state-manager.html}
|
|
39
|
+
*
|
|
40
|
+
* ```
|
|
41
|
+
* // server-side
|
|
42
|
+
* import { Server } from '@soundworks/server/index.js';
|
|
43
|
+
*
|
|
44
|
+
* const server = new Server(config);
|
|
45
|
+
* // declare and register the schema of a shared state.
|
|
46
|
+
* server.stateManager.registerSchema('some-global-state', {
|
|
47
|
+
* myRandom: {
|
|
48
|
+
* type: 'float',
|
|
49
|
+
* default: 0,
|
|
50
|
+
* }
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* await server.start();
|
|
54
|
+
*
|
|
55
|
+
* // create a global state server-side
|
|
56
|
+
* const globalState = await server.stateManager.create('some-global-state');
|
|
57
|
+
* // listen and react to the changes made by the clients
|
|
58
|
+
* globalState.onUpdate(updates => console.log(updates));
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* ```
|
|
62
|
+
* // client-side
|
|
63
|
+
* import { Client } from '@soundworks/client.index.js';
|
|
64
|
+
*
|
|
65
|
+
* const client = new Client(config);
|
|
66
|
+
* await client.start();
|
|
67
|
+
*
|
|
68
|
+
* // attach to the global state created by the server
|
|
69
|
+
* const globalState = await client.stateManager.attach('some-global-state');
|
|
70
|
+
*
|
|
71
|
+
* // update the value of a `myRandom` parameter every seconds
|
|
72
|
+
* setInterval(() => {
|
|
73
|
+
* globalState.set({ myRandom: Math.random() });
|
|
74
|
+
* }, 1000);
|
|
75
|
+
* ``
|
|
76
|
+
*
|
|
77
|
+
* @memberof server
|
|
78
|
+
* @extends BaseSharedState
|
|
79
|
+
* @inheritdoc
|
|
80
|
+
* @hideconstructor
|
|
81
|
+
* @see {server.StateManager}
|
|
82
|
+
*/
|
|
83
|
+
class SharedState extends BaseSharedState {}
|
|
84
|
+
|
|
85
|
+
export default SharedState;
|
|
86
|
+
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
|
|
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
|
+
// const CONNECTING = 0;
|
|
10
|
+
// const OPEN = 1;
|
|
11
|
+
// const CLOSING = 2;
|
|
12
|
+
// const CLOSED = 3;
|
|
13
|
+
// const READY_STATES = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
|
2
14
|
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _socketsEncoderDecoder = require("../common/sockets-encoder-decoder.js");
|
|
9
|
-
|
|
10
|
-
const noop = () => {};
|
|
11
|
-
|
|
12
|
-
let counter = 0;
|
|
13
|
-
const CONNECTING = 0;
|
|
14
|
-
const OPEN = 1;
|
|
15
|
-
const CLOSING = 2;
|
|
16
|
-
const CLOSED = 3;
|
|
17
|
-
const READY_STATES = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
|
18
15
|
/**
|
|
19
16
|
* Simple wrapper with simple pubsub system built on top of `ws` sockets.
|
|
20
17
|
* The abstraction contains two different socket:
|
|
@@ -26,9 +23,9 @@ const READY_STATES = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
|
|
26
23
|
*
|
|
27
24
|
* @memberof server
|
|
28
25
|
*/
|
|
29
|
-
|
|
30
26
|
class Socket {
|
|
31
27
|
constructor(ws, binaryWs, rooms, sockets, options = {}) {
|
|
28
|
+
|
|
32
29
|
/**
|
|
33
30
|
* Reference to the sockets object, is mainly dedicated to allow
|
|
34
31
|
* broadcasting from a given socket instance.
|
|
@@ -37,122 +34,163 @@ class Socket {
|
|
|
37
34
|
* socket.sockets.broadcast('my-room', this, 'update-value', 1);
|
|
38
35
|
*/
|
|
39
36
|
this.sockets = sockets;
|
|
37
|
+
|
|
40
38
|
/**
|
|
41
39
|
* `ws` socket instance configured with `binaryType=blob` (string)
|
|
42
40
|
* @private
|
|
43
41
|
* @type {Object}
|
|
44
42
|
*/
|
|
45
|
-
|
|
46
43
|
this.ws = ws;
|
|
44
|
+
|
|
47
45
|
/**
|
|
48
46
|
* `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
|
|
49
47
|
* @private
|
|
50
48
|
* @type {Object}
|
|
51
49
|
*/
|
|
52
|
-
|
|
53
50
|
this.binaryWs = binaryWs;
|
|
51
|
+
|
|
54
52
|
/**
|
|
55
53
|
* `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
|
|
56
54
|
* @private
|
|
57
55
|
* @type {Map}
|
|
58
56
|
*/
|
|
59
|
-
|
|
60
57
|
this.rooms = rooms;
|
|
58
|
+
|
|
61
59
|
/**
|
|
62
60
|
* Configuration object
|
|
63
61
|
* @type {Object}
|
|
64
62
|
*/
|
|
65
|
-
|
|
66
63
|
this.config = {
|
|
67
64
|
pingInterval: 5 * 1000,
|
|
68
|
-
...options
|
|
65
|
+
...options,
|
|
69
66
|
};
|
|
67
|
+
|
|
70
68
|
this._stringListeners = new Map();
|
|
71
|
-
this._binaryListeners = new Map();
|
|
69
|
+
this._binaryListeners = new Map();
|
|
70
|
+
|
|
71
|
+
// ----------------------------------------------------------
|
|
72
72
|
// init string socket
|
|
73
73
|
// ----------------------------------------------------------
|
|
74
|
-
|
|
75
74
|
this.ws.addEventListener('message', e => {
|
|
76
|
-
const [channel, args] =
|
|
77
|
-
|
|
75
|
+
const [channel, args] = unpackStringMessage(e.data);
|
|
78
76
|
this._emit(false, channel, ...args);
|
|
79
|
-
});
|
|
77
|
+
});
|
|
80
78
|
|
|
81
|
-
|
|
79
|
+
// broadcast all `ws` "native" events
|
|
80
|
+
[
|
|
81
|
+
'close',
|
|
82
|
+
'error',
|
|
83
|
+
'message',
|
|
84
|
+
'open',
|
|
85
|
+
'ping',
|
|
86
|
+
'pong',
|
|
87
|
+
'unexpected-response',
|
|
88
|
+
'upgrade',
|
|
89
|
+
].forEach(eventName => {
|
|
82
90
|
this.ws.addEventListener(eventName, e => {
|
|
83
91
|
this._emit(false, eventName, e.data);
|
|
84
92
|
});
|
|
85
|
-
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// ----------------------------------------------------------
|
|
86
96
|
// init binary socket
|
|
87
97
|
// ----------------------------------------------------------
|
|
88
|
-
|
|
89
98
|
this.binaryWs.addEventListener('message', e => {
|
|
90
|
-
const [channel, data] =
|
|
91
|
-
|
|
99
|
+
const [channel, data] = unpackBinaryMessage(e.data);
|
|
92
100
|
this._emit(true, channel, data);
|
|
93
|
-
});
|
|
101
|
+
});
|
|
94
102
|
|
|
95
|
-
|
|
103
|
+
// broadcast all `ws` "native" events
|
|
104
|
+
[
|
|
105
|
+
'close',
|
|
106
|
+
'error',
|
|
107
|
+
'message',
|
|
108
|
+
'open',
|
|
109
|
+
'ping',
|
|
110
|
+
'pong',
|
|
111
|
+
'unexpected-response',
|
|
112
|
+
'upgrade',
|
|
113
|
+
].forEach(eventName => {
|
|
96
114
|
this.binaryWs.addEventListener(eventName, e => {
|
|
97
115
|
this._emit(true, eventName, e.data);
|
|
98
116
|
});
|
|
99
|
-
});
|
|
100
|
-
// https://github.com/websockets/ws#how-to-detect-and-close-broken-connections
|
|
117
|
+
});
|
|
101
118
|
|
|
102
|
-
|
|
119
|
+
// heartbeat system (run only on string socket), adapted from:
|
|
120
|
+
// https://github.com/websockets/ws#how-to-detect-and-close-broken-connections
|
|
121
|
+
this._isAlive = true;
|
|
122
|
+
let msg = {
|
|
123
|
+
type: 'add-measurement',
|
|
124
|
+
value: {
|
|
125
|
+
ping: 0,
|
|
126
|
+
pong: 0,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
103
129
|
|
|
130
|
+
// heartbeat system, only on "regular" socket
|
|
104
131
|
this.ws.on('pong', () => {
|
|
105
132
|
this._isAlive = true;
|
|
133
|
+
|
|
134
|
+
msg.value.pong = getTime();
|
|
135
|
+
this.sockets._latencyStatsWorker.postMessage(msg);
|
|
106
136
|
});
|
|
137
|
+
|
|
107
138
|
this._intervalId = setInterval(() => {
|
|
108
139
|
if (this._isAlive === false) {
|
|
109
140
|
// emit a 'close' event to go trough all the disconnection pipeline
|
|
110
141
|
this._emit(false, 'close');
|
|
111
|
-
|
|
112
|
-
return; // return this.ws.terminate();
|
|
142
|
+
return;
|
|
113
143
|
}
|
|
114
144
|
|
|
115
145
|
this._isAlive = false;
|
|
116
|
-
|
|
146
|
+
msg.value.ping = getTime();
|
|
147
|
+
|
|
148
|
+
this.ws.ping();
|
|
117
149
|
}, this.config.pingInterval);
|
|
118
|
-
this.ws.addListener('error', err => {// console.log(this.clientId, err);
|
|
119
|
-
});
|
|
120
150
|
}
|
|
151
|
+
|
|
121
152
|
/**
|
|
122
153
|
* Called when the string socket closes (aka client reload).
|
|
123
154
|
*/
|
|
124
|
-
|
|
125
155
|
/** @private */
|
|
126
|
-
|
|
127
|
-
|
|
128
156
|
terminate() {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
157
|
+
// clear ping/pong check
|
|
158
|
+
clearInterval(this._intervalId);
|
|
159
|
+
// clean rooms
|
|
160
|
+
for (let [_key, room] of this.rooms) {
|
|
132
161
|
room.delete(this);
|
|
133
|
-
}
|
|
134
|
-
|
|
162
|
+
}
|
|
135
163
|
|
|
164
|
+
// clear references to sockets and rooms
|
|
136
165
|
this.sockets = null;
|
|
137
|
-
this.rooms = null;
|
|
166
|
+
this.rooms = null;
|
|
138
167
|
|
|
168
|
+
// clear all listeners
|
|
139
169
|
this._stringListeners.clear();
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
170
|
+
this._binaryListeners.clear();
|
|
171
|
+
|
|
172
|
+
// clear "native" listeners
|
|
173
|
+
[this.binaryWs, this.ws].forEach((socket) => {
|
|
174
|
+
[
|
|
175
|
+
'close',
|
|
176
|
+
'error',
|
|
177
|
+
'message',
|
|
178
|
+
'open',
|
|
179
|
+
'ping',
|
|
180
|
+
'pong',
|
|
181
|
+
'unexpected-response',
|
|
182
|
+
'upgrade',
|
|
183
|
+
].forEach(eventName => {
|
|
146
184
|
socket.removeAllListeners(eventName);
|
|
147
185
|
});
|
|
148
|
-
});
|
|
186
|
+
});
|
|
149
187
|
|
|
188
|
+
// clear binarySocket as this is called from the string one.
|
|
150
189
|
this.binaryWs.terminate();
|
|
151
190
|
this.ws.terminate();
|
|
152
191
|
}
|
|
153
|
-
/** @private */
|
|
154
|
-
|
|
155
192
|
|
|
193
|
+
/** @private */
|
|
156
194
|
_emit(binary, channel, ...args) {
|
|
157
195
|
const listeners = binary ? this._binaryListeners : this._stringListeners;
|
|
158
196
|
|
|
@@ -161,9 +199,8 @@ class Socket {
|
|
|
161
199
|
callbacks.forEach(callback => callback(...args));
|
|
162
200
|
}
|
|
163
201
|
}
|
|
164
|
-
/** @private */
|
|
165
|
-
|
|
166
202
|
|
|
203
|
+
/** @private */
|
|
167
204
|
_addListener(listeners, channel, callback) {
|
|
168
205
|
if (!listeners.has(channel)) {
|
|
169
206
|
listeners.set(channel, new Set());
|
|
@@ -172,9 +209,8 @@ class Socket {
|
|
|
172
209
|
const callbacks = listeners.get(channel);
|
|
173
210
|
callbacks.add(callback);
|
|
174
211
|
}
|
|
175
|
-
/** @private */
|
|
176
|
-
|
|
177
212
|
|
|
213
|
+
/** @private */
|
|
178
214
|
_removeListener(listeners, channel, callback) {
|
|
179
215
|
if (listeners.has(channel)) {
|
|
180
216
|
const callbacks = listeners.get(channel);
|
|
@@ -185,20 +221,18 @@ class Socket {
|
|
|
185
221
|
}
|
|
186
222
|
}
|
|
187
223
|
}
|
|
188
|
-
/** @private */
|
|
189
|
-
|
|
190
224
|
|
|
225
|
+
/** @private */
|
|
191
226
|
_removeAllListeners(listeners, channel) {
|
|
192
227
|
if (listeners.has(channel)) {
|
|
193
228
|
listeners.delete(channel);
|
|
194
229
|
}
|
|
195
230
|
}
|
|
231
|
+
|
|
196
232
|
/**
|
|
197
233
|
* Add the socket to a room
|
|
198
234
|
* @param {String} roomId - Id of the room
|
|
199
235
|
*/
|
|
200
|
-
|
|
201
|
-
|
|
202
236
|
addToRoom(roomId) {
|
|
203
237
|
if (!this.rooms.has(roomId)) {
|
|
204
238
|
this.rooms.set(roomId, new Set());
|
|
@@ -207,122 +241,113 @@ class Socket {
|
|
|
207
241
|
const room = this.rooms.get(roomId);
|
|
208
242
|
room.add(this);
|
|
209
243
|
}
|
|
244
|
+
|
|
210
245
|
/**
|
|
211
246
|
* Remove the socket from a room
|
|
212
247
|
* @param {String} roomId - Id of the room
|
|
213
248
|
*/
|
|
214
|
-
|
|
215
|
-
|
|
216
249
|
removeFromRoom(roomId) {
|
|
217
250
|
if (this.rooms.has(roomId)) {
|
|
218
251
|
const room = this.rooms.get(roomId);
|
|
219
252
|
room.delete(this);
|
|
220
253
|
}
|
|
221
254
|
}
|
|
255
|
+
|
|
222
256
|
/**
|
|
223
257
|
* Sends JSON compatible messages on a given channel
|
|
224
258
|
*
|
|
225
259
|
* @param {String} channel - Channel of the message
|
|
226
260
|
* @param {...*} args - Arguments of the message (as many as needed, of any type)
|
|
227
261
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
230
262
|
send(channel, ...args) {
|
|
231
|
-
const msg =
|
|
263
|
+
const msg = packStringMessage(channel, ...args);
|
|
264
|
+
|
|
265
|
+
// 0 CONNECTING Socket has been created. The connection is not yet open.
|
|
232
266
|
// 1 OPEN The connection is open and ready to communicate.
|
|
233
267
|
// 2 CLOSING The connection is in the process of closing.
|
|
234
268
|
// 3 CLOSED The connection is closed or couldn't be opened.
|
|
235
|
-
|
|
236
269
|
if (this.ws.readyState === 1) {
|
|
237
|
-
this.ws.send(msg, err => {
|
|
270
|
+
this.ws.send(msg, (err) => {
|
|
238
271
|
if (err) {
|
|
239
272
|
console.error('error sending msg:', channel, args, err);
|
|
240
273
|
}
|
|
241
274
|
});
|
|
242
275
|
}
|
|
243
276
|
}
|
|
277
|
+
|
|
244
278
|
/**
|
|
245
279
|
* Listen JSON compatible messages on a given channel
|
|
246
280
|
*
|
|
247
281
|
* @param {String} channel - Channel of the message
|
|
248
282
|
* @param {Function} callback - Callback to execute when a message is received
|
|
249
283
|
*/
|
|
250
|
-
|
|
251
|
-
|
|
252
284
|
addListener(channel, callback) {
|
|
253
285
|
this._addListener(this._stringListeners, channel, callback);
|
|
254
286
|
}
|
|
287
|
+
|
|
255
288
|
/**
|
|
256
289
|
* Remove a listener from JSON compatible messages on a given channel
|
|
257
290
|
*
|
|
258
291
|
* @param {String} channel - Channel of the message
|
|
259
292
|
* @param {Function} callback - Callback to cancel
|
|
260
293
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
294
|
removeListener(channel, callback) {
|
|
264
295
|
this._removeListener(this._stringListeners, channel, callback);
|
|
265
296
|
}
|
|
297
|
+
|
|
266
298
|
/**
|
|
267
299
|
* Remove all listeners from JSON compatible messages on a given channel
|
|
268
300
|
*
|
|
269
301
|
* @param {String} channel - Channel of the message
|
|
270
302
|
*/
|
|
271
|
-
|
|
272
|
-
|
|
273
303
|
removeAllListeners(channel) {
|
|
274
304
|
this._removeAllListeners(this._stringListeners, channel);
|
|
275
305
|
}
|
|
306
|
+
|
|
276
307
|
/**
|
|
277
308
|
* Sends binary messages on a given channel
|
|
278
309
|
*
|
|
279
310
|
* @param {String} channel - Channel of the message
|
|
280
311
|
* @param {TypedArray} typedArray - Data to send
|
|
281
312
|
*/
|
|
282
|
-
|
|
283
|
-
|
|
284
313
|
sendBinary(channel, typedArray) {
|
|
285
|
-
const msg =
|
|
286
|
-
|
|
314
|
+
const msg = packBinaryMessage(channel, typedArray);
|
|
315
|
+
|
|
316
|
+
this.binaryWs.send(msg, (err) => {
|
|
287
317
|
if (err) {
|
|
288
318
|
console.error('error sending msg:', channel, typedArray);
|
|
289
319
|
}
|
|
290
320
|
});
|
|
291
321
|
}
|
|
322
|
+
|
|
292
323
|
/**
|
|
293
324
|
* Listen binary messages on a given channel
|
|
294
325
|
*
|
|
295
326
|
* @param {String} channel - Channel of the message
|
|
296
327
|
* @param {Function} callback - Callback to execute when a message is received
|
|
297
328
|
*/
|
|
298
|
-
|
|
299
|
-
|
|
300
329
|
addBinaryListener(channel, callback) {
|
|
301
330
|
this._addListener(this._binaryListeners, channel, callback);
|
|
302
331
|
}
|
|
332
|
+
|
|
303
333
|
/**
|
|
304
334
|
* Remove a listener from binary compatible messages on a given channel
|
|
305
335
|
*
|
|
306
336
|
* @param {String} channel - Channel of the message
|
|
307
337
|
* @param {Function} callback - Callback to cancel
|
|
308
338
|
*/
|
|
309
|
-
|
|
310
|
-
|
|
311
339
|
removeBinaryListener(channel, callback) {
|
|
312
340
|
this._removeListener(this._binaryListeners, channel, callback);
|
|
313
341
|
}
|
|
342
|
+
|
|
314
343
|
/**
|
|
315
344
|
* Remove all listeners from binary compatible messages on a given channel
|
|
316
345
|
*
|
|
317
346
|
* @param {String} channel - Channel of the message
|
|
318
347
|
*/
|
|
319
|
-
|
|
320
|
-
|
|
321
348
|
removeAllBinaryListeners(channel) {
|
|
322
349
|
this._removeAllListeners(this._binaryListeners, channel);
|
|
323
350
|
}
|
|
324
|
-
|
|
325
351
|
}
|
|
326
352
|
|
|
327
|
-
|
|
328
|
-
exports.default = _default;
|
|
353
|
+
export default Socket;
|