@rpgjs/client 5.0.0-alpha.10 → 5.0.0-alpha.11
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/dist/Game/Map.d.ts +4 -0
- package/dist/Gui/Gui.d.ts +82 -5
- package/dist/RpgClient.d.ts +35 -5
- package/dist/RpgClientEngine.d.ts +3 -0
- package/dist/components/gui/index.d.ts +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/index10.js +105 -4
- package/dist/index10.js.map +1 -1
- package/dist/index11.js +20 -7
- package/dist/index11.js.map +1 -1
- package/dist/index12.js +6 -8
- package/dist/index12.js.map +1 -1
- package/dist/index13.js +9 -14
- package/dist/index13.js.map +1 -1
- package/dist/index14.js +8 -100
- package/dist/index14.js.map +1 -1
- package/dist/index15.js +141 -35
- package/dist/index15.js.map +1 -1
- package/dist/index16.js +45 -186
- package/dist/index16.js.map +1 -1
- package/dist/index17.js +229 -5
- package/dist/index17.js.map +1 -1
- package/dist/index18.js +5 -383
- package/dist/index18.js.map +1 -1
- package/dist/index19.js +384 -28
- package/dist/index19.js.map +1 -1
- package/dist/index2.js +15 -8
- package/dist/index2.js.map +1 -1
- package/dist/index20.js +31 -17
- package/dist/index20.js.map +1 -1
- package/dist/index21.js +17 -2413
- package/dist/index21.js.map +1 -1
- package/dist/index22.js +2587 -88
- package/dist/index22.js.map +1 -1
- package/dist/index23.js +108 -103
- package/dist/index23.js.map +1 -1
- package/dist/index29.js.map +1 -1
- package/dist/index3.js +2 -2
- package/dist/index30.js +14 -1
- package/dist/index30.js.map +1 -1
- package/dist/index37.js +56 -169
- package/dist/index37.js.map +1 -1
- package/dist/index38.js +162 -489
- package/dist/index38.js.map +1 -1
- package/dist/index39.js +496 -56
- package/dist/index39.js.map +1 -1
- package/dist/index4.js +2 -2
- package/dist/index41.js +21 -7
- package/dist/index41.js.map +1 -1
- package/dist/index5.js +1 -1
- package/dist/index6.js +1 -1
- package/dist/index7.js +1 -1
- package/dist/index8.js +1 -1
- package/dist/index9.js +105 -14
- package/dist/index9.js.map +1 -1
- package/dist/services/mmorpg.d.ts +1 -1
- package/dist/services/standalone.d.ts +1 -1
- package/package.json +7 -5
- package/src/Game/AnimationManager.ts +1 -0
- package/src/Game/Map.ts +9 -1
- package/src/Game/Object.ts +28 -6
- package/src/Gui/Gui.ts +141 -16
- package/src/RpgClient.ts +36 -5
- package/src/RpgClientEngine.ts +11 -1
- package/src/components/character.ce +11 -9
- package/src/components/gui/box.ce +17 -0
- package/src/components/gui/dialogbox/index.ce +1 -0
- package/src/components/gui/index.ts +3 -4
- package/src/components/scenes/event-layer.ce +6 -0
- package/src/index.ts +2 -1
package/dist/index38.js
CHANGED
|
@@ -1,501 +1,174 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// biome-ignore lint/style/useDefaultParameterLast: legacy
|
|
28
|
-
// biome-ignore lint/suspicious/noExplicitAny: legacy
|
|
29
|
-
constructor(code = 1e3, reason = "", target) {
|
|
30
|
-
super("close", target);
|
|
31
|
-
this.code = code;
|
|
32
|
-
this.reason = reason;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
var Events = {
|
|
36
|
-
Event,
|
|
37
|
-
ErrorEvent,
|
|
38
|
-
CloseEvent
|
|
39
|
-
};
|
|
40
|
-
function assert(condition, msg) {
|
|
41
|
-
if (!condition) {
|
|
42
|
-
throw new Error(msg);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function cloneEventBrowser(e) {
|
|
46
|
-
return new e.constructor(e.type, e);
|
|
1
|
+
import { ReconnectingWebSocket } from './index39.js';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var valueIsNotNil = (keyValuePair) =>
|
|
5
|
+
keyValuePair[1] !== null && keyValuePair[1] !== void 0;
|
|
6
|
+
function generateUUID() {
|
|
7
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
8
|
+
return crypto.randomUUID();
|
|
9
|
+
}
|
|
10
|
+
let d = /* @__PURE__ */ new Date().getTime();
|
|
11
|
+
let d2 =
|
|
12
|
+
(typeof performance !== "undefined" &&
|
|
13
|
+
performance.now &&
|
|
14
|
+
performance.now() * 1e3) ||
|
|
15
|
+
0;
|
|
16
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
17
|
+
let r = Math.random() * 16;
|
|
18
|
+
if (d > 0) {
|
|
19
|
+
r = (d + r) % 16 | 0;
|
|
20
|
+
d = Math.floor(d / 16);
|
|
21
|
+
} else {
|
|
22
|
+
r = (d2 + r) % 16 | 0;
|
|
23
|
+
d2 = Math.floor(d2 / 16);
|
|
24
|
+
}
|
|
25
|
+
return (c === "x" ? r : (r & 3) | 8).toString(16);
|
|
26
|
+
});
|
|
47
27
|
}
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
if ("
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
28
|
+
function getPartyInfo(partySocketOptions, defaultProtocol, defaultParams = {}) {
|
|
29
|
+
const {
|
|
30
|
+
host: rawHost,
|
|
31
|
+
path: rawPath,
|
|
32
|
+
protocol: rawProtocol,
|
|
33
|
+
room,
|
|
34
|
+
party,
|
|
35
|
+
basePath,
|
|
36
|
+
prefix,
|
|
37
|
+
query
|
|
38
|
+
} = partySocketOptions;
|
|
39
|
+
let host = rawHost.replace(/^(http|https|ws|wss):\/\//, "");
|
|
40
|
+
if (host.endsWith("/")) {
|
|
41
|
+
host = host.slice(0, -1);
|
|
42
|
+
}
|
|
43
|
+
if (rawPath == null ? void 0 : rawPath.startsWith("/")) {
|
|
44
|
+
throw new Error("path must not start with a slash");
|
|
45
|
+
}
|
|
46
|
+
const name = party ?? "main";
|
|
47
|
+
const path = rawPath ? `/${rawPath}` : "";
|
|
48
|
+
const protocol =
|
|
49
|
+
rawProtocol ||
|
|
50
|
+
(host.startsWith("localhost:") ||
|
|
51
|
+
host.startsWith("127.0.0.1:") ||
|
|
52
|
+
host.startsWith("192.168.") ||
|
|
53
|
+
host.startsWith("10.") ||
|
|
54
|
+
(host.startsWith("172.") &&
|
|
55
|
+
host.split(".")[1] >= "16" &&
|
|
56
|
+
host.split(".")[1] <= "31") ||
|
|
57
|
+
host.startsWith("[::ffff:7f00:1]:")
|
|
58
|
+
? // http / ws
|
|
59
|
+
defaultProtocol
|
|
60
|
+
: // https / wss
|
|
61
|
+
`${defaultProtocol}s`);
|
|
62
|
+
const baseUrl = `${protocol}://${host}/${basePath || `${prefix || "parties"}/${name}/${room}`}${path}`;
|
|
63
|
+
const makeUrl = (query2 = {}) =>
|
|
64
|
+
`${baseUrl}?${new URLSearchParams([
|
|
65
|
+
...Object.entries(defaultParams),
|
|
66
|
+
...Object.entries(query2).filter(valueIsNotNil)
|
|
67
|
+
])}`;
|
|
68
|
+
const urlProvider =
|
|
69
|
+
typeof query === "function"
|
|
70
|
+
? async () => makeUrl(await query())
|
|
71
|
+
: makeUrl(query);
|
|
72
|
+
return {
|
|
73
|
+
host,
|
|
74
|
+
path,
|
|
75
|
+
room,
|
|
76
|
+
name,
|
|
77
|
+
protocol,
|
|
78
|
+
partyUrl: baseUrl,
|
|
79
|
+
urlProvider
|
|
80
|
+
};
|
|
69
81
|
}
|
|
70
|
-
var
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
104
|
-
if (this._options.startClosed) {
|
|
105
|
-
this._shouldReconnect = false;
|
|
106
|
-
}
|
|
107
|
-
if (this._options.debugLogger) {
|
|
108
|
-
this._debugLogger = this._options.debugLogger;
|
|
109
|
-
}
|
|
110
|
-
this._connect();
|
|
111
|
-
}
|
|
112
|
-
static get CONNECTING() {
|
|
113
|
-
return 0;
|
|
114
|
-
}
|
|
115
|
-
static get OPEN() {
|
|
116
|
-
return 1;
|
|
117
|
-
}
|
|
118
|
-
static get CLOSING() {
|
|
119
|
-
return 2;
|
|
120
|
-
}
|
|
121
|
-
static get CLOSED() {
|
|
122
|
-
return 3;
|
|
123
|
-
}
|
|
124
|
-
get CONNECTING() {
|
|
125
|
-
return _ReconnectingWebSocket.CONNECTING;
|
|
126
|
-
}
|
|
127
|
-
get OPEN() {
|
|
128
|
-
return _ReconnectingWebSocket.OPEN;
|
|
129
|
-
}
|
|
130
|
-
get CLOSING() {
|
|
131
|
-
return _ReconnectingWebSocket.CLOSING;
|
|
132
|
-
}
|
|
133
|
-
get CLOSED() {
|
|
134
|
-
return _ReconnectingWebSocket.CLOSED;
|
|
135
|
-
}
|
|
136
|
-
get binaryType() {
|
|
137
|
-
return this._ws ? this._ws.binaryType : this._binaryType;
|
|
138
|
-
}
|
|
139
|
-
set binaryType(value) {
|
|
140
|
-
this._binaryType = value;
|
|
141
|
-
if (this._ws) {
|
|
142
|
-
this._ws.binaryType = value;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Returns the number or connection retries
|
|
147
|
-
*/
|
|
148
|
-
get retryCount() {
|
|
149
|
-
return Math.max(this._retryCount, 0);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* The number of bytes of data that have been queued using calls to send() but not yet
|
|
153
|
-
* transmitted to the network. This value resets to zero once all queued data has been sent.
|
|
154
|
-
* This value does not reset to zero when the connection is closed; if you keep calling send(),
|
|
155
|
-
* this will continue to climb. Read only
|
|
156
|
-
*/
|
|
157
|
-
get bufferedAmount() {
|
|
158
|
-
const bytes = this._messageQueue.reduce((acc, message) => {
|
|
159
|
-
if (typeof message === "string") {
|
|
160
|
-
acc += message.length;
|
|
161
|
-
} else if (message instanceof Blob) {
|
|
162
|
-
acc += message.size;
|
|
163
|
-
} else {
|
|
164
|
-
acc += message.byteLength;
|
|
165
|
-
}
|
|
166
|
-
return acc;
|
|
167
|
-
}, 0);
|
|
168
|
-
return bytes + (this._ws ? this._ws.bufferedAmount : 0);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* The extensions selected by the server. This is currently only the empty string or a list of
|
|
172
|
-
* extensions as negotiated by the connection
|
|
173
|
-
*/
|
|
174
|
-
get extensions() {
|
|
175
|
-
return this._ws ? this._ws.extensions : "";
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* A string indicating the name of the sub-protocol the server selected;
|
|
179
|
-
* this will be one of the strings specified in the protocols parameter when creating the
|
|
180
|
-
* WebSocket object
|
|
181
|
-
*/
|
|
182
|
-
get protocol() {
|
|
183
|
-
return this._ws ? this._ws.protocol : "";
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* The current state of the connection; this is one of the Ready state constants
|
|
187
|
-
*/
|
|
188
|
-
get readyState() {
|
|
189
|
-
if (this._ws) {
|
|
190
|
-
return this._ws.readyState;
|
|
191
|
-
}
|
|
192
|
-
return this._options.startClosed
|
|
193
|
-
? _ReconnectingWebSocket.CLOSED
|
|
194
|
-
: _ReconnectingWebSocket.CONNECTING;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* The URL as resolved by the constructor
|
|
198
|
-
*/
|
|
199
|
-
get url() {
|
|
200
|
-
return this._ws ? this._ws.url : "";
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Whether the websocket object is now in reconnectable state
|
|
204
|
-
*/
|
|
205
|
-
get shouldReconnect() {
|
|
206
|
-
return this._shouldReconnect;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
|
|
210
|
-
*/
|
|
211
|
-
onclose = null;
|
|
212
|
-
/**
|
|
213
|
-
* An event listener to be called when an error occurs
|
|
214
|
-
*/
|
|
215
|
-
onerror = null;
|
|
216
|
-
/**
|
|
217
|
-
* An event listener to be called when a message is received from the server
|
|
218
|
-
*/
|
|
219
|
-
onmessage = null;
|
|
220
|
-
/**
|
|
221
|
-
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
|
|
222
|
-
* this indicates that the connection is ready to send and receive data
|
|
223
|
-
*/
|
|
224
|
-
onopen = null;
|
|
225
|
-
/**
|
|
226
|
-
* Closes the WebSocket connection or connection attempt, if any. If the connection is already
|
|
227
|
-
* CLOSED, this method does nothing
|
|
228
|
-
*/
|
|
229
|
-
close(code = 1e3, reason) {
|
|
230
|
-
this._closeCalled = true;
|
|
231
|
-
this._shouldReconnect = false;
|
|
232
|
-
this._clearTimeouts();
|
|
233
|
-
if (!this._ws) {
|
|
234
|
-
this._debug("close enqueued: no ws instance");
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
if (this._ws.readyState === this.CLOSED) {
|
|
238
|
-
this._debug("close: already closed");
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
this._ws.close(code, reason);
|
|
82
|
+
var PartySocket = class extends ReconnectingWebSocket {
|
|
83
|
+
constructor(partySocketOptions) {
|
|
84
|
+
const wsOptions = getWSOptions(partySocketOptions);
|
|
85
|
+
super(wsOptions.urlProvider, wsOptions.protocols, wsOptions.socketOptions);
|
|
86
|
+
this.partySocketOptions = partySocketOptions;
|
|
87
|
+
this.setWSProperties(wsOptions);
|
|
88
|
+
}
|
|
89
|
+
_pk;
|
|
90
|
+
_pkurl;
|
|
91
|
+
name;
|
|
92
|
+
room;
|
|
93
|
+
host;
|
|
94
|
+
path;
|
|
95
|
+
updateProperties(partySocketOptions) {
|
|
96
|
+
const wsOptions = getWSOptions({
|
|
97
|
+
...this.partySocketOptions,
|
|
98
|
+
...partySocketOptions,
|
|
99
|
+
host: partySocketOptions.host ?? this.host,
|
|
100
|
+
room: partySocketOptions.room ?? this.room,
|
|
101
|
+
path: partySocketOptions.path ?? this.path
|
|
102
|
+
});
|
|
103
|
+
this._url = wsOptions.urlProvider;
|
|
104
|
+
this._protocols = wsOptions.protocols;
|
|
105
|
+
this._options = wsOptions.socketOptions;
|
|
106
|
+
this.setWSProperties(wsOptions);
|
|
107
|
+
}
|
|
108
|
+
setWSProperties(wsOptions) {
|
|
109
|
+
const { _pk, _pkurl, name, room, host, path } = wsOptions;
|
|
110
|
+
this._pk = _pk;
|
|
111
|
+
this._pkurl = _pkurl;
|
|
112
|
+
this.name = name;
|
|
113
|
+
this.room = room;
|
|
114
|
+
this.host = host;
|
|
115
|
+
this.path = path;
|
|
242
116
|
}
|
|
243
|
-
/**
|
|
244
|
-
* Closes the WebSocket connection or connection attempt and connects again.
|
|
245
|
-
* Resets retry counter;
|
|
246
|
-
*/
|
|
247
117
|
reconnect(code, reason) {
|
|
248
|
-
this.
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
this._connect();
|
|
253
|
-
} else {
|
|
254
|
-
this._disconnect(code, reason);
|
|
255
|
-
this._connect();
|
|
118
|
+
if (!this.room || !this.host) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
"The room and host must be set before connecting, use `updateProperties` method to set them or pass them to the constructor."
|
|
121
|
+
);
|
|
256
122
|
}
|
|
123
|
+
super.reconnect(code, reason);
|
|
124
|
+
}
|
|
125
|
+
get id() {
|
|
126
|
+
return this._pk;
|
|
257
127
|
}
|
|
258
128
|
/**
|
|
259
|
-
*
|
|
129
|
+
* Exposes the static PartyKit room URL without applying query parameters.
|
|
130
|
+
* To access the currently connected WebSocket url, use PartySocket#url.
|
|
260
131
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
_debug(...args) {
|
|
275
|
-
if (this._options.debug) {
|
|
276
|
-
this._debugLogger("RWS>", ...args);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
_getNextDelay() {
|
|
280
|
-
const {
|
|
281
|
-
reconnectionDelayGrowFactor = DEFAULT.reconnectionDelayGrowFactor,
|
|
282
|
-
minReconnectionDelay = DEFAULT.minReconnectionDelay,
|
|
283
|
-
maxReconnectionDelay = DEFAULT.maxReconnectionDelay
|
|
284
|
-
} = this._options;
|
|
285
|
-
let delay = 0;
|
|
286
|
-
if (this._retryCount > 0) {
|
|
287
|
-
delay =
|
|
288
|
-
minReconnectionDelay *
|
|
289
|
-
reconnectionDelayGrowFactor ** (this._retryCount - 1);
|
|
290
|
-
if (delay > maxReconnectionDelay) {
|
|
291
|
-
delay = maxReconnectionDelay;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
this._debug("next delay", delay);
|
|
295
|
-
return delay;
|
|
296
|
-
}
|
|
297
|
-
_wait() {
|
|
298
|
-
return new Promise((resolve) => {
|
|
299
|
-
setTimeout(resolve, this._getNextDelay());
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
_getNextProtocols(protocolsProvider) {
|
|
303
|
-
if (!protocolsProvider) return Promise.resolve(null);
|
|
304
|
-
if (
|
|
305
|
-
typeof protocolsProvider === "string" ||
|
|
306
|
-
Array.isArray(protocolsProvider)
|
|
307
|
-
) {
|
|
308
|
-
return Promise.resolve(protocolsProvider);
|
|
309
|
-
}
|
|
310
|
-
if (typeof protocolsProvider === "function") {
|
|
311
|
-
const protocols = protocolsProvider();
|
|
312
|
-
if (!protocols) return Promise.resolve(null);
|
|
313
|
-
if (typeof protocols === "string" || Array.isArray(protocols)) {
|
|
314
|
-
return Promise.resolve(protocols);
|
|
315
|
-
}
|
|
316
|
-
if (protocols.then) {
|
|
317
|
-
return protocols;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
throw Error("Invalid protocols");
|
|
321
|
-
}
|
|
322
|
-
_getNextUrl(urlProvider) {
|
|
323
|
-
if (typeof urlProvider === "string") {
|
|
324
|
-
return Promise.resolve(urlProvider);
|
|
325
|
-
}
|
|
326
|
-
if (typeof urlProvider === "function") {
|
|
327
|
-
const url = urlProvider();
|
|
328
|
-
if (typeof url === "string") {
|
|
329
|
-
return Promise.resolve(url);
|
|
330
|
-
}
|
|
331
|
-
if (url.then) {
|
|
332
|
-
return url;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
throw Error("Invalid URL");
|
|
336
|
-
}
|
|
337
|
-
_connect() {
|
|
338
|
-
if (this._connectLock || !this._shouldReconnect) {
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
this._connectLock = true;
|
|
342
|
-
const {
|
|
343
|
-
maxRetries = DEFAULT.maxRetries,
|
|
344
|
-
connectionTimeout = DEFAULT.connectionTimeout
|
|
345
|
-
} = this._options;
|
|
346
|
-
if (this._retryCount >= maxRetries) {
|
|
347
|
-
this._debug("max retries reached", this._retryCount, ">=", maxRetries);
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
this._retryCount++;
|
|
351
|
-
this._debug("connect", this._retryCount);
|
|
352
|
-
this._removeListeners();
|
|
353
|
-
this._wait()
|
|
354
|
-
.then(() =>
|
|
355
|
-
Promise.all([
|
|
356
|
-
this._getNextUrl(this._url),
|
|
357
|
-
this._getNextProtocols(this._protocols || null)
|
|
358
|
-
])
|
|
359
|
-
)
|
|
360
|
-
.then(([url, protocols]) => {
|
|
361
|
-
if (this._closeCalled) {
|
|
362
|
-
this._connectLock = false;
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
if (
|
|
366
|
-
!this._options.WebSocket &&
|
|
367
|
-
typeof WebSocket === "undefined" &&
|
|
368
|
-
!didWarnAboutMissingWebSocket
|
|
369
|
-
) {
|
|
370
|
-
console.error(`\u203C\uFE0F No WebSocket implementation available. You should define options.WebSocket.
|
|
371
|
-
|
|
372
|
-
For example, if you're using node.js, run \`npm install ws\`, and then in your code:
|
|
373
|
-
|
|
374
|
-
import PartySocket from 'partysocket';
|
|
375
|
-
import WS from 'ws';
|
|
376
|
-
|
|
377
|
-
const partysocket = new PartySocket({
|
|
378
|
-
host: "127.0.0.1:1999",
|
|
379
|
-
room: "test-room",
|
|
380
|
-
WebSocket: WS
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
`);
|
|
384
|
-
didWarnAboutMissingWebSocket = true;
|
|
385
|
-
}
|
|
386
|
-
const WS = this._options.WebSocket || WebSocket;
|
|
387
|
-
this._debug("connect", { url, protocols });
|
|
388
|
-
this._ws = protocols ? new WS(url, protocols) : new WS(url);
|
|
389
|
-
this._ws.binaryType = this._binaryType;
|
|
390
|
-
this._connectLock = false;
|
|
391
|
-
this._addListeners();
|
|
392
|
-
this._connectTimeout = setTimeout(
|
|
393
|
-
() => this._handleTimeout(),
|
|
394
|
-
connectionTimeout
|
|
395
|
-
);
|
|
396
|
-
})
|
|
397
|
-
.catch((err) => {
|
|
398
|
-
this._connectLock = false;
|
|
399
|
-
this._handleError(new Events.ErrorEvent(Error(err.message), this));
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
_handleTimeout() {
|
|
403
|
-
this._debug("timeout event");
|
|
404
|
-
this._handleError(new Events.ErrorEvent(Error("TIMEOUT"), this));
|
|
405
|
-
}
|
|
406
|
-
_disconnect(code = 1e3, reason) {
|
|
407
|
-
this._clearTimeouts();
|
|
408
|
-
if (!this._ws) {
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
this._removeListeners();
|
|
412
|
-
try {
|
|
413
|
-
if (this._ws.readyState === this.OPEN) {
|
|
414
|
-
this._ws.close(code, reason);
|
|
415
|
-
}
|
|
416
|
-
this._handleClose(new Events.CloseEvent(code, reason, this));
|
|
417
|
-
} catch (error) {}
|
|
418
|
-
}
|
|
419
|
-
_acceptOpen() {
|
|
420
|
-
this._debug("accept open");
|
|
421
|
-
this._retryCount = 0;
|
|
422
|
-
}
|
|
423
|
-
_handleOpen = (event) => {
|
|
424
|
-
this._debug("open event");
|
|
425
|
-
const { minUptime = DEFAULT.minUptime } = this._options;
|
|
426
|
-
clearTimeout(this._connectTimeout);
|
|
427
|
-
this._uptimeTimeout = setTimeout(() => this._acceptOpen(), minUptime);
|
|
428
|
-
assert(this._ws, "WebSocket is not defined");
|
|
429
|
-
this._ws.binaryType = this._binaryType;
|
|
430
|
-
this._messageQueue.forEach((message) => {
|
|
431
|
-
var _a2;
|
|
432
|
-
return (_a2 = this._ws) == null ? void 0 : _a2.send(message);
|
|
433
|
-
});
|
|
434
|
-
this._messageQueue = [];
|
|
435
|
-
if (this.onopen) {
|
|
436
|
-
this.onopen(event);
|
|
437
|
-
}
|
|
438
|
-
this.dispatchEvent(cloneEvent(event));
|
|
439
|
-
};
|
|
440
|
-
_handleMessage = (event) => {
|
|
441
|
-
this._debug("message event");
|
|
442
|
-
if (this.onmessage) {
|
|
443
|
-
this.onmessage(event);
|
|
444
|
-
}
|
|
445
|
-
this.dispatchEvent(cloneEvent(event));
|
|
446
|
-
};
|
|
447
|
-
_handleError = (event) => {
|
|
448
|
-
this._debug("error event", event.message);
|
|
449
|
-
this._disconnect(void 0, event.message === "TIMEOUT" ? "timeout" : void 0);
|
|
450
|
-
if (this.onerror) {
|
|
451
|
-
this.onerror(event);
|
|
452
|
-
}
|
|
453
|
-
this._debug("exec error listeners");
|
|
454
|
-
this.dispatchEvent(cloneEvent(event));
|
|
455
|
-
this._connect();
|
|
456
|
-
};
|
|
457
|
-
_handleClose = (event) => {
|
|
458
|
-
this._debug("close event");
|
|
459
|
-
this._clearTimeouts();
|
|
460
|
-
if (this._shouldReconnect) {
|
|
461
|
-
this._connect();
|
|
462
|
-
}
|
|
463
|
-
if (this.onclose) {
|
|
464
|
-
this.onclose(event);
|
|
465
|
-
}
|
|
466
|
-
this.dispatchEvent(cloneEvent(event));
|
|
467
|
-
};
|
|
468
|
-
_removeListeners() {
|
|
469
|
-
if (!this._ws) {
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
this._debug("removeListeners");
|
|
473
|
-
this._ws.removeEventListener("open", this._handleOpen);
|
|
474
|
-
this._ws.removeEventListener("close", this._handleClose);
|
|
475
|
-
this._ws.removeEventListener("message", this._handleMessage);
|
|
476
|
-
this._ws.removeEventListener("error", this._handleError);
|
|
477
|
-
}
|
|
478
|
-
_addListeners() {
|
|
479
|
-
if (!this._ws) {
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
this._debug("addListeners");
|
|
483
|
-
this._ws.addEventListener("open", this._handleOpen);
|
|
484
|
-
this._ws.addEventListener("close", this._handleClose);
|
|
485
|
-
this._ws.addEventListener("message", this._handleMessage);
|
|
486
|
-
this._ws.addEventListener("error", this._handleError);
|
|
487
|
-
}
|
|
488
|
-
_clearTimeouts() {
|
|
489
|
-
clearTimeout(this._connectTimeout);
|
|
490
|
-
clearTimeout(this._uptimeTimeout);
|
|
132
|
+
get roomUrl() {
|
|
133
|
+
return this._pkurl;
|
|
134
|
+
}
|
|
135
|
+
// a `fetch` method that uses (almost) the same options as `PartySocket`
|
|
136
|
+
static async fetch(options, init) {
|
|
137
|
+
const party = getPartyInfo(options, "http");
|
|
138
|
+
const url =
|
|
139
|
+
typeof party.urlProvider === "string"
|
|
140
|
+
? party.urlProvider
|
|
141
|
+
: await party.urlProvider();
|
|
142
|
+
const doFetch = options.fetch ?? fetch;
|
|
143
|
+
return doFetch(url, init);
|
|
491
144
|
}
|
|
492
145
|
};
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
146
|
+
function getWSOptions(partySocketOptions) {
|
|
147
|
+
const {
|
|
148
|
+
id,
|
|
149
|
+
host: _host,
|
|
150
|
+
path: _path,
|
|
151
|
+
party: _party,
|
|
152
|
+
room: _room,
|
|
153
|
+
protocol: _protocol,
|
|
154
|
+
query: _query,
|
|
155
|
+
protocols,
|
|
156
|
+
...socketOptions
|
|
157
|
+
} = partySocketOptions;
|
|
158
|
+
const _pk = id || generateUUID();
|
|
159
|
+
const party = getPartyInfo(partySocketOptions, "ws", { _pk });
|
|
160
|
+
return {
|
|
161
|
+
_pk,
|
|
162
|
+
_pkurl: party.partyUrl,
|
|
163
|
+
name: party.name,
|
|
164
|
+
room: party.room,
|
|
165
|
+
host: party.host,
|
|
166
|
+
path: party.path,
|
|
167
|
+
protocols,
|
|
168
|
+
socketOptions,
|
|
169
|
+
urlProvider: party.urlProvider
|
|
170
|
+
};
|
|
171
|
+
}
|
|
499
172
|
|
|
500
|
-
export {
|
|
173
|
+
export { PartySocket };
|
|
501
174
|
//# sourceMappingURL=index38.js.map
|