teckos-client 0.3.3 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/ITeckosClient.d.ts +18 -0
- package/dist/TeckosClient.d.ts +35 -0
- package/dist/TeckosClientWithJWT.d.ts +12 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +40 -43
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +1 -1
- package/dist/teckos-client.cjs.development.js +611 -0
- package/dist/teckos-client.cjs.development.js.map +1 -0
- package/dist/teckos-client.cjs.production.min.js +2 -0
- package/dist/teckos-client.cjs.production.min.js.map +1 -0
- package/dist/teckos-client.esm.js +608 -0
- package/dist/teckos-client.esm.js.map +1 -0
- package/dist/types/ConnectionState.d.ts +7 -0
- package/dist/types/Options.d.ts +10 -0
- package/dist/types/Packet.d.ts +6 -0
- package/dist/types/PacketType.d.ts +5 -0
- package/dist/types/SocketEvent.d.ts +10 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/util/Converter.d.ts +4 -0
- package/dist/util/SocketEventEmitter.d.ts +23 -0
- package/es/index.js +34 -39
- package/es/index.mjs +1 -1
- package/lib/index.js +37 -58
- package/package.json +6 -7
- package/src/ITeckosClient.ts +2 -3
- package/src/TeckosClient.ts +29 -29
- package/src/TeckosClientWithJWT.ts +6 -6
- package/src/index.ts +3 -3
- package/types/ITeckosClient.d.ts +2 -3
- package/types/TeckosClient.d.ts +5 -5
- package/types/index.d.ts +3 -3
- package/src/ITeckosClient.js +0 -2
- package/src/ITeckosClient.js.map +0 -1
- package/src/TeckosClient.js +0 -235
- package/src/TeckosClient.js.map +0 -1
- package/src/TeckosClientWithJWT.js +0 -63
- package/src/TeckosClientWithJWT.js.map +0 -1
- package/src/index.js +0 -8
- package/src/index.js.map +0 -1
- package/src/types/ConnectionState.js +0 -9
- package/src/types/ConnectionState.js.map +0 -1
- package/src/types/Options.js +0 -2
- package/src/types/Options.js.map +0 -1
- package/src/types/Packet.js +0 -2
- package/src/types/Packet.js.map +0 -1
- package/src/types/PacketType.js +0 -7
- package/src/types/PacketType.js.map +0 -1
- package/src/types/SocketEvent.js +0 -2
- package/src/types/SocketEvent.js.map +0 -1
- package/src/types/index.js +0 -4
- package/src/types/index.js.map +0 -1
- package/src/util/Converter.js +0 -6
- package/src/util/Converter.js.map +0 -1
- package/src/util/SocketEventEmitter.js +0 -99
- package/src/util/SocketEventEmitter.js.map +0 -1
@@ -0,0 +1,18 @@
|
|
1
|
+
/// <reference types="ws" />
|
2
|
+
import WebSocket from 'isomorphic-ws';
|
3
|
+
import { ConnectionState, Packet, SocketEvent } from './types';
|
4
|
+
import { SocketEventEmitter } from './util/SocketEventEmitter';
|
5
|
+
interface ITeckosClient extends SocketEventEmitter<SocketEvent> {
|
6
|
+
ws: WebSocket | undefined;
|
7
|
+
readonly webSocket: WebSocket | undefined;
|
8
|
+
readonly state: ConnectionState;
|
9
|
+
readonly connected: boolean;
|
10
|
+
readonly disconnected: boolean;
|
11
|
+
emit: (event: SocketEvent, ...args: any[]) => boolean;
|
12
|
+
send: (...args: any[]) => boolean;
|
13
|
+
sendPackage: (packet: Packet) => boolean;
|
14
|
+
close: () => void;
|
15
|
+
connect: () => void;
|
16
|
+
disconnect: () => void;
|
17
|
+
}
|
18
|
+
export type { ITeckosClient };
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/// <reference types="ws" />
|
2
|
+
import WebSocket from 'isomorphic-ws';
|
3
|
+
import { ConnectionState, OptionalOptions, Options, Packet, SocketEvent } from './types';
|
4
|
+
import { ITeckosClient } from './ITeckosClient';
|
5
|
+
import { SocketEventEmitter } from './util/SocketEventEmitter';
|
6
|
+
declare class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosClient {
|
7
|
+
protected readonly url: string;
|
8
|
+
protected readonly options: Options;
|
9
|
+
ws: WebSocket | undefined;
|
10
|
+
protected currentReconnectDelay: number;
|
11
|
+
protected currentReconnectionAttempts: number;
|
12
|
+
protected acks: Map<number, (...args: any[]) => void>;
|
13
|
+
protected fnId: number;
|
14
|
+
protected connectionTimeout: any | undefined;
|
15
|
+
protected reconnectionTimeout: any | undefined;
|
16
|
+
constructor(url: string, options?: OptionalOptions);
|
17
|
+
protected attachHandler: () => void;
|
18
|
+
get webSocket(): WebSocket | undefined;
|
19
|
+
connect: () => void;
|
20
|
+
protected reconnect: () => void;
|
21
|
+
protected getConnectionState(): ConnectionState;
|
22
|
+
get state(): ConnectionState;
|
23
|
+
get connected(): boolean;
|
24
|
+
get disconnected(): boolean;
|
25
|
+
emit: (event: SocketEvent, ...args: any[]) => boolean;
|
26
|
+
send: (...args: any[]) => boolean;
|
27
|
+
sendPackage: (packet: Packet) => boolean;
|
28
|
+
protected handleMessage: (msg: WebSocket.MessageEvent) => void;
|
29
|
+
protected handleOpen: () => void;
|
30
|
+
protected handleError: (error: WebSocket.ErrorEvent) => void;
|
31
|
+
protected handleClose: () => void;
|
32
|
+
close: () => void;
|
33
|
+
disconnect: () => void;
|
34
|
+
}
|
35
|
+
export { TeckosClient };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { TeckosClient } from './TeckosClient';
|
2
|
+
import { OptionalOptions, ConnectionState } from './types';
|
3
|
+
declare class TeckosClientWithJWT extends TeckosClient {
|
4
|
+
protected readonly token: string;
|
5
|
+
protected readonly initialData: any;
|
6
|
+
protected receivedReady: boolean;
|
7
|
+
constructor(url: string, options: OptionalOptions, token: string, initialData?: any);
|
8
|
+
protected getConnectionState(): ConnectionState;
|
9
|
+
protected handleReadyEvent: () => void;
|
10
|
+
protected handleOpen: () => void;
|
11
|
+
}
|
12
|
+
export { TeckosClientWithJWT };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import { TeckosClientWithJWT } from './TeckosClientWithJWT';
|
2
|
+
import { TeckosClient } from './TeckosClient';
|
3
|
+
import { ITeckosClient } from './ITeckosClient';
|
4
|
+
import { ConnectionState, OptionalOptions, Options, Packet, PacketType, SocketEvent } from './types';
|
5
|
+
/**
|
6
|
+
* Expose all types
|
7
|
+
*/
|
8
|
+
export type { Options, OptionalOptions, Packet, SocketEvent, ITeckosClient };
|
9
|
+
export { ConnectionState, PacketType, TeckosClient, TeckosClientWithJWT };
|
package/dist/index.js
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
(function (global, factory) {
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["Teckos Client"] = {}));
|
5
|
-
})(this, (function (exports) { 'use strict';
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('isomorphic-ws')) :
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'isomorphic-ws'], factory) :
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["Teckos Client"] = {}, global["Isomorphic WS"]));
|
5
|
+
})(this, (function (exports, WebSocket) { 'use strict';
|
6
6
|
|
7
|
-
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
8
|
+
|
9
|
+
var WebSocket__default = /*#__PURE__*/_interopDefaultLegacy(WebSocket);
|
8
10
|
|
9
11
|
const enc = new TextEncoder();
|
10
12
|
const dec = new TextDecoder();
|
@@ -154,7 +156,7 @@ const DEFAULT_OPTIONS = {
|
|
154
156
|
class TeckosClient extends SocketEventEmitter {
|
155
157
|
url;
|
156
158
|
options;
|
157
|
-
|
159
|
+
_ws;
|
158
160
|
currentReconnectDelay;
|
159
161
|
currentReconnectionAttempts = 0;
|
160
162
|
acks = new Map();
|
@@ -172,31 +174,26 @@ class TeckosClient extends SocketEventEmitter {
|
|
172
174
|
}
|
173
175
|
|
174
176
|
attachHandler = () => {
|
175
|
-
if (this.
|
176
|
-
this.
|
177
|
-
this.
|
178
|
-
this.
|
179
|
-
this.
|
177
|
+
if (this._ws) {
|
178
|
+
this._ws.onopen = this.handleOpen;
|
179
|
+
this._ws.onerror = this.handleError;
|
180
|
+
this._ws.onclose = this.handleClose;
|
181
|
+
this._ws.onmessage = this.handleMessage;
|
180
182
|
}
|
181
183
|
};
|
182
|
-
|
183
|
-
get webSocket() {
|
184
|
-
return this.ws;
|
185
|
-
}
|
186
|
-
|
187
184
|
connect = () => {
|
188
185
|
if (this.options.debug) console.log(`(teckos:client) Connecting to ${this.url}...`); // This will try to connect immediately
|
189
186
|
// eslint-disable-next-line new-cap
|
190
187
|
|
191
|
-
this.
|
188
|
+
this._ws = new WebSocket__default["default"](this.url); // Attach handlers
|
192
189
|
|
193
190
|
this.attachHandler(); // Handle timeout
|
194
191
|
|
195
192
|
this.connectionTimeout = setTimeout(() => {
|
196
|
-
if (this.
|
193
|
+
if (this._ws && this._ws.readyState === 0
|
197
194
|
/* = CONNECTING */
|
198
195
|
) {
|
199
|
-
this.
|
196
|
+
this._ws.close();
|
200
197
|
}
|
201
198
|
}, this.options.timeout);
|
202
199
|
};
|
@@ -206,25 +203,18 @@ class TeckosClient extends SocketEventEmitter {
|
|
206
203
|
};
|
207
204
|
|
208
205
|
getConnectionState() {
|
209
|
-
if (this.
|
210
|
-
switch (this.
|
211
|
-
case
|
212
|
-
/* = CONNECTING */
|
213
|
-
:
|
214
|
-
return exports.ConnectionState.CONNECTING;
|
215
|
-
|
216
|
-
case 1
|
217
|
-
/* = OPEN */
|
218
|
-
:
|
206
|
+
if (this._ws) {
|
207
|
+
switch (this._ws.readyState) {
|
208
|
+
case WebSocket__default["default"].OPEN:
|
219
209
|
return exports.ConnectionState.CONNECTED;
|
220
210
|
|
221
|
-
case
|
222
|
-
|
223
|
-
|
211
|
+
case WebSocket__default["default"].CONNECTING:
|
212
|
+
return exports.ConnectionState.CONNECTING;
|
213
|
+
|
214
|
+
case WebSocket__default["default"].CLOSING:
|
224
215
|
return exports.ConnectionState.DISCONNECTING;
|
225
216
|
|
226
217
|
default:
|
227
|
-
/* 3 = CLOSED */
|
228
218
|
return exports.ConnectionState.DISCONNECTED;
|
229
219
|
}
|
230
220
|
}
|
@@ -236,6 +226,10 @@ class TeckosClient extends SocketEventEmitter {
|
|
236
226
|
return this.getConnectionState();
|
237
227
|
}
|
238
228
|
|
229
|
+
get ws() {
|
230
|
+
return this._ws;
|
231
|
+
}
|
232
|
+
|
239
233
|
get connected() {
|
240
234
|
return this.getConnectionState() === exports.ConnectionState.CONNECTED;
|
241
235
|
}
|
@@ -268,12 +262,14 @@ class TeckosClient extends SocketEventEmitter {
|
|
268
262
|
});
|
269
263
|
};
|
270
264
|
sendPackage = packet => {
|
271
|
-
if (this.
|
265
|
+
if (this._ws !== undefined && this._ws.readyState === 1
|
272
266
|
/* = OPEN */
|
273
267
|
) {
|
274
268
|
const buffer = encodePacket(packet);
|
275
269
|
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Send packet: ${JSON.stringify(packet)}`);
|
276
|
-
|
270
|
+
|
271
|
+
this._ws.send(buffer);
|
272
|
+
|
277
273
|
return true;
|
278
274
|
}
|
279
275
|
|
@@ -373,10 +369,11 @@ class TeckosClient extends SocketEventEmitter {
|
|
373
369
|
close = () => {
|
374
370
|
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Closing connection (client-side)`);
|
375
371
|
|
376
|
-
if (this.
|
377
|
-
this.
|
372
|
+
if (this._ws !== undefined) {
|
373
|
+
this._ws.onclose = () => {};
|
374
|
+
|
375
|
+
this._ws.close();
|
378
376
|
|
379
|
-
this.ws.close();
|
380
377
|
this.listeners('disconnect').forEach(listener => listener());
|
381
378
|
}
|
382
379
|
};
|
@@ -403,19 +400,19 @@ class TeckosClientWithJWT extends TeckosClient {
|
|
403
400
|
}
|
404
401
|
|
405
402
|
getConnectionState() {
|
406
|
-
if (this.
|
407
|
-
switch (this.
|
408
|
-
case
|
403
|
+
if (this._ws) {
|
404
|
+
switch (this._ws.readyState) {
|
405
|
+
case WebSocket__default["default"].OPEN:
|
409
406
|
if (this.receivedReady) {
|
410
407
|
return exports.ConnectionState.CONNECTED;
|
411
408
|
}
|
412
409
|
|
413
410
|
return exports.ConnectionState.CONNECTING;
|
414
411
|
|
415
|
-
case
|
412
|
+
case WebSocket__default["default"].CONNECTING:
|
416
413
|
return exports.ConnectionState.CONNECTING;
|
417
414
|
|
418
|
-
case
|
415
|
+
case WebSocket__default["default"].CLOSING:
|
419
416
|
return exports.ConnectionState.DISCONNECTING;
|
420
417
|
|
421
418
|
default:
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../example/node/index.ts"],"names":[],"mappings":"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"}
|
package/dist/index.min.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["Teckos Client"]={})}(this,(function(e){"use strict";function t(e){return`Minified Redux error #${e}; visit https://redux.js.org/Errors?code=${e} for the full message or use the non-minified dev environment for full errors. `}
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("isomorphic-ws")):"function"==typeof define&&define.amd?define(["exports","isomorphic-ws"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["Teckos Client"]={},e["Isomorphic WS"])}(this,(function(e,t){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=n(t);function i(e){return`Minified Redux error #${e}; visit https://redux.js.org/Errors?code=${e} for the full message or use the non-minified dev environment for full errors. `}const o=new TextEncoder,c=new TextDecoder;var r,h;e.PacketType=void 0,(r=e.PacketType||(e.PacketType={}))[r.EVENT=0]="EVENT",r[r.ACK=1]="ACK",e.ConnectionState=void 0,(h=e.ConnectionState||(e.ConnectionState={})).DISCONNECTED="disconnected",h.CONNECTING="connecting",h.CONNECTED="connected",h.DISCONNECTING="disconnecting";const a={reconnection:!0,reconnectionDelay:1e3,reconnectionDelayMax:5e3,reconnectionAttempts:1/0,randomizationFactor:.5,timeout:5e3,debug:!1};class l extends class{maxListeners=50;handlers={};addListener=(e,t)=>{if(Object.keys(this.handlers).length===this.maxListeners)throw Error(i(2));if("function"!=typeof t)throw Error(i(3));return this.handlers[e]=this.handlers[e]||[],this.handlers[e].push(t),this};once=(e,t)=>{if(Object.keys(this.handlers).length===this.maxListeners)throw Error(i(2));if("function"!=typeof t)throw Error(i(3));this.handlers[e]=this.handlers[e]||[];const n=()=>{t(),this.off(e,n)};return this.handlers[e].push(n),this};removeListener=(e,t)=>(this.handlers[e]&&(this.handlers[e]=this.handlers[e].filter((e=>e!==t))),this);off=(e,t)=>this.removeListener(e,t);removeAllListeners=e=>(e?delete this.handlers[e]:this.handlers={},this);setMaxListeners=e=>(this.maxListeners=e,this);getMaxListeners=()=>this.maxListeners;listeners=e=>this.handlers[e]?[...this.handlers[e]]:[];rawListeners=e=>[...this.handlers[e]];listenerCount=e=>this.handlers[e]?Object.keys(this.handlers[e]).length:0;prependListener=(e,t)=>{if(Object.keys(this.handlers).length===this.maxListeners)throw Error(i(2));return this.handlers[e]=this.handlers[e]||[],this.handlers[e].unshift(t),this};prependOnceListener=(e,t)=>{if(Object.keys(this.handlers).length===this.maxListeners)throw Error(i(2));this.handlers[e]=this.handlers[e]||[];const n=()=>{t(),this.off(e,n)};return this.handlers[e].unshift(n),this};eventNames=()=>Object.keys(this.handlers);on=(e,t)=>this.addListener(e,t);emit=(e,...t)=>{const n=this.listeners(e);return n.length>0&&(n.forEach((e=>{e&&e(t)})),!0)}}{url;options;_ws;currentReconnectDelay;currentReconnectionAttempts=0;acks=new Map;fnId=0;connectionTimeout;reconnectionTimeout;constructor(e,t){super(),this.options={...a,...t},this.currentReconnectDelay=this.options.reconnectionDelay,this.url=e}attachHandler=()=>{this._ws&&(this._ws.onopen=this.handleOpen,this._ws.onerror=this.handleError,this._ws.onclose=this.handleClose,this._ws.onmessage=this.handleMessage)};connect=()=>{this.options.debug&&console.log(`(teckos:client) Connecting to ${this.url}...`),this._ws=new s.default(this.url),this.attachHandler(),this.connectionTimeout=setTimeout((()=>{this._ws&&0===this._ws.readyState&&this._ws.close()}),this.options.timeout)};reconnect=()=>{this.listeners("reconnect_attempt").forEach((e=>e())),this.connect()};getConnectionState(){if(this._ws)switch(this._ws.readyState){case s.default.OPEN:return e.ConnectionState.CONNECTED;case s.default.CONNECTING:return e.ConnectionState.CONNECTING;case s.default.CLOSING:return e.ConnectionState.DISCONNECTING;default:return e.ConnectionState.DISCONNECTED}return e.ConnectionState.DISCONNECTED}get state(){return this.getConnectionState()}get ws(){return this._ws}get connected(){return this.getConnectionState()===e.ConnectionState.CONNECTED}get disconnected(){return this.getConnectionState()===e.ConnectionState.DISCONNECTED}emit=(t,...n)=>{n.unshift(t);const s={type:e.PacketType.EVENT,data:n};return"function"==typeof n[n.length-1]&&(this.acks.set(this.fnId,n.pop()),s.id=this.fnId,this.fnId+=1),this.sendPackage(s)};send=(...t)=>(t.unshift("message"),this.sendPackage({type:e.PacketType.EVENT,data:t}));sendPackage=e=>{if(void 0!==this._ws&&1===this._ws.readyState){const t=(e=>o.encode(JSON.stringify(e)))(e);return this.options.debug&&console.log(`(teckos:client) [${this.url}] Send packet: ${JSON.stringify(e)}`),this._ws.send(t),!0}return!1};handleMessage=t=>{const n="string"==typeof t.data?JSON.parse(t.data):JSON.parse(""+c.decode(t.data));if(this.options.debug&&console.log(`(teckos:client) [${this.url}] Got packet: ${JSON.stringify(n)}`),n.type===e.PacketType.EVENT){const e=n.data[0],t=n.data.slice(1);if(!e)throw Error(i(0));this.listeners(e).forEach((e=>e(...t)))}else{if(n.type!==e.PacketType.ACK||void 0===n.id)throw Error(i(1));{const e=this.acks.get(n.id);"function"==typeof e&&(e.apply(this,n.data),this.acks.delete(n.id))}}};handleOpen=()=>{this.currentReconnectionAttempts>0&&(this.currentReconnectDelay=this.options.reconnectionDelay,this.currentReconnectionAttempts=0,this.options.debug&&console.log(`(teckos:client) [${this.url}] Reconnected!`),this.listeners("reconnect").forEach((e=>e()))),this.options.debug&&console.log(`(teckos:client) [${this.url}] Connected!`),this.listeners("connect").forEach((e=>e()))};handleError=e=>{this.handlers&&this.handlers.error&&(this.options.debug&&console.log(`(teckos:client) [${this.url}] Got error from server: ${JSON.stringify(e)}`),this.handlers.error.forEach((t=>t(e))))};handleClose=()=>{if(this.connectionTimeout&&clearTimeout(this.connectionTimeout),this.reconnectionTimeout&&clearTimeout(this.reconnectionTimeout),this.currentReconnectionAttempts>0?(this.options.debug&&console.log(`(teckos:client) [${this.url}] Reconnect #${this.currentReconnectionAttempts} failed!`),this.listeners("reconnect_error").forEach((e=>{e&&e()}))):(this.options.debug&&console.log(`(teckos:client) [${this.url}] Disconnected!`),this.listeners("disconnect").forEach((e=>{e&&e()}))),this.options.reconnection)if(this.currentReconnectionAttempts+=1,this.options.reconnectionAttempts!==1/0&&this.currentReconnectionAttempts>this.options.reconnectionAttempts)this.options.debug&&console.log(`(teckos:client) [${this.url}] Reconnection maximum of ${this.options.reconnectionAttempts} reached`),this.listeners("reconnect_failed").forEach((e=>e()));else{const e=Math.min(this.options.reconnectionDelayMax,this.currentReconnectDelay);this.currentReconnectDelay=Math.round(this.currentReconnectDelay+this.currentReconnectDelay*this.options.randomizationFactor),this.options.debug&&console.log(`(teckos:client) [${this.url}] Try reconnecting (${this.currentReconnectionAttempts}/${this.options.reconnectionAttempts}) in ${e}ms to ${this.url}...`),this.reconnectionTimeout=setTimeout((()=>{this.reconnect()}),e)}};close=()=>{this.options.debug&&console.log(`(teckos:client) [${this.url}] Closing connection (client-side)`),void 0!==this._ws&&(this._ws.onclose=()=>{},this._ws.close(),this.listeners("disconnect").forEach((e=>e())))};disconnect=()=>{this.close()}}e.TeckosClient=l,e.TeckosClientWithJWT=class extends l{token;initialData;receivedReady=!1;constructor(e,t,n,s){super(e,t),this.token=n,this.initialData=s,this.on("disconnect",(()=>{this.receivedReady=!1}))}getConnectionState(){if(this._ws)switch(this._ws.readyState){case s.default.OPEN:return this.receivedReady?e.ConnectionState.CONNECTED:e.ConnectionState.CONNECTING;case s.default.CONNECTING:return e.ConnectionState.CONNECTING;case s.default.CLOSING:return e.ConnectionState.DISCONNECTING;default:return e.ConnectionState.DISCONNECTED}return e.ConnectionState.DISCONNECTED}handleReadyEvent=()=>{this.options.debug&&console.log(`(teckos:client) [${this.url}] Connected!`),this.receivedReady=!0,this.currentReconnectionAttempts>0&&(this.options.debug&&console.log(`(teckos:client) [${this.url}] Reconnected!`),this.listeners("reconnect").forEach((e=>e())),this.currentReconnectDelay=this.options.reconnectionDelay,this.currentReconnectionAttempts=0),this.listeners("connect").forEach((e=>e()))};handleOpen=()=>{this.receivedReady=!1,this.once("ready",this.handleReadyEvent),this.options.debug&&console.log("(teckos:client) Connection opened, sending token now"),this.emit("token",{token:this.token,...this.initialData})}},Object.defineProperty(e,"__esModule",{value:!0})}));
|