teckos-client 0.2.3 → 0.3.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/dist/index.js +456 -5
- package/dist/index.min.js +1 -0
- package/es/index.js +459 -0
- package/es/index.mjs +1 -0
- package/lib/index.js +484 -0
- package/package.json +63 -44
- package/src/ITeckosClient.js +2 -0
- package/src/ITeckosClient.js.map +1 -0
- package/src/ITeckosClient.ts +3 -3
- package/src/TeckosClient.js +235 -0
- package/src/TeckosClient.js.map +1 -0
- package/src/TeckosClient.ts +40 -32
- package/src/TeckosClientWithJWT.js +63 -0
- package/src/TeckosClientWithJWT.js.map +1 -0
- package/src/TeckosClientWithJWT.ts +8 -10
- package/src/index.js +8 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +3 -3
- package/src/types/ConnectionState.js +9 -0
- package/src/types/ConnectionState.js.map +1 -0
- package/src/types/Options.js +2 -0
- package/src/types/Options.js.map +1 -0
- package/src/types/Packet.js +2 -0
- package/src/types/Packet.js.map +1 -0
- package/src/types/PacketType.js +7 -0
- package/src/types/PacketType.js.map +1 -0
- package/src/types/SocketEvent.js +2 -0
- package/src/types/SocketEvent.js.map +1 -0
- package/src/types/index.js +4 -0
- package/src/types/index.js.map +1 -0
- package/src/util/Converter.js +6 -0
- package/src/util/Converter.js.map +1 -0
- package/src/util/SocketEventEmitter.js +99 -0
- package/src/util/SocketEventEmitter.js.map +1 -0
- package/src/util/formatProdErrorMessage.ts +16 -0
- package/{dist → types}/ITeckosClient.d.ts +3 -3
- package/{dist → types}/TeckosClient.d.ts +6 -6
- package/{dist → types}/TeckosClientWithJWT.d.ts +0 -0
- package/types/index.d.ts +9 -0
- package/{dist → types}/types/ConnectionState.d.ts +0 -0
- package/{dist → types}/types/Options.d.ts +0 -0
- package/{dist → types}/types/Packet.d.ts +0 -0
- package/{dist → types}/types/PacketType.d.ts +0 -0
- package/{dist → types}/types/SocketEvent.d.ts +0 -0
- package/{dist → types}/types/index.d.ts +0 -0
- package/{dist → types}/util/Converter.d.ts +0 -0
- package/{dist → types}/util/SocketEventEmitter.d.ts +1 -1
- package/types/util/formatProdErrorMessage.d.ts +9 -0
- package/dist/index.d.ts +0 -9
- package/dist/teckos-client.cjs.development.js +0 -614
- package/dist/teckos-client.cjs.development.js.map +0 -1
- package/dist/teckos-client.cjs.production.min.js +0 -2
- package/dist/teckos-client.cjs.production.min.js.map +0 -1
- package/dist/teckos-client.esm.js +0 -611
- package/dist/teckos-client.esm.js.map +0 -1
@@ -0,0 +1,235 @@
|
|
1
|
+
/* eslint-disable no-console */
|
2
|
+
import * as IsomorphicWebSocket from 'isomorphic-ws';
|
3
|
+
import { decodePacket, encodePacket } from './util/Converter';
|
4
|
+
import { SocketEventEmitter } from './util/SocketEventEmitter';
|
5
|
+
import { ConnectionState, PacketType } from './types';
|
6
|
+
const DEFAULT_OPTIONS = {
|
7
|
+
reconnection: true,
|
8
|
+
reconnectionDelay: 1000,
|
9
|
+
reconnectionDelayMax: 5000,
|
10
|
+
reconnectionAttempts: Infinity,
|
11
|
+
randomizationFactor: 0.5,
|
12
|
+
timeout: 5000,
|
13
|
+
debug: false,
|
14
|
+
};
|
15
|
+
class TeckosClient extends SocketEventEmitter {
|
16
|
+
url;
|
17
|
+
options;
|
18
|
+
ws;
|
19
|
+
currentReconnectDelay;
|
20
|
+
currentReconnectionAttempts = 0;
|
21
|
+
acks = new Map();
|
22
|
+
fnId = 0;
|
23
|
+
connectionTimeout;
|
24
|
+
reconnectionTimeout;
|
25
|
+
constructor(url, options) {
|
26
|
+
super();
|
27
|
+
this.options = {
|
28
|
+
...DEFAULT_OPTIONS,
|
29
|
+
...options,
|
30
|
+
};
|
31
|
+
this.currentReconnectDelay = this.options.reconnectionDelay;
|
32
|
+
this.url = url;
|
33
|
+
}
|
34
|
+
attachHandler = () => {
|
35
|
+
if (this.ws) {
|
36
|
+
this.ws.onopen = this.handleOpen;
|
37
|
+
this.ws.onerror = this.handleError;
|
38
|
+
this.ws.onclose = this.handleClose;
|
39
|
+
this.ws.onmessage = this.handleMessage;
|
40
|
+
}
|
41
|
+
};
|
42
|
+
get webSocket() {
|
43
|
+
return this.ws;
|
44
|
+
}
|
45
|
+
connect = () => {
|
46
|
+
if (this.options.debug)
|
47
|
+
console.log(`(teckos:client) Connecting to ${this.url}...`);
|
48
|
+
// This will try to connect immediately
|
49
|
+
// eslint-disable-next-line new-cap
|
50
|
+
this.ws = new IsomorphicWebSocket.WebSocket(this.url);
|
51
|
+
// Attach handlers
|
52
|
+
this.attachHandler();
|
53
|
+
// Handle timeout
|
54
|
+
this.connectionTimeout = setTimeout(() => {
|
55
|
+
if (this.ws && this.ws.readyState === 0 /* = CONNECTING */) {
|
56
|
+
this.ws.close();
|
57
|
+
}
|
58
|
+
}, this.options.timeout);
|
59
|
+
};
|
60
|
+
reconnect = () => {
|
61
|
+
this.listeners('reconnect_attempt').forEach((listener) => listener());
|
62
|
+
this.connect();
|
63
|
+
};
|
64
|
+
getConnectionState() {
|
65
|
+
if (this.ws) {
|
66
|
+
switch (this.ws.readyState) {
|
67
|
+
case 0 /* = CONNECTING */:
|
68
|
+
return ConnectionState.CONNECTING;
|
69
|
+
case 1 /* = OPEN */:
|
70
|
+
return ConnectionState.CONNECTED;
|
71
|
+
case 2 /* = CLOSING */:
|
72
|
+
return ConnectionState.DISCONNECTING;
|
73
|
+
default: /* 3 = CLOSED */
|
74
|
+
return ConnectionState.DISCONNECTED;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
return ConnectionState.DISCONNECTED;
|
78
|
+
}
|
79
|
+
get state() {
|
80
|
+
return this.getConnectionState();
|
81
|
+
}
|
82
|
+
get connected() {
|
83
|
+
return this.getConnectionState() === ConnectionState.CONNECTED;
|
84
|
+
}
|
85
|
+
get disconnected() {
|
86
|
+
return this.getConnectionState() === ConnectionState.DISCONNECTED;
|
87
|
+
}
|
88
|
+
emit = (event, ...args) => {
|
89
|
+
args.unshift(event);
|
90
|
+
const packet = {
|
91
|
+
type: PacketType.EVENT,
|
92
|
+
data: args,
|
93
|
+
};
|
94
|
+
if (typeof args[args.length - 1] === 'function') {
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
96
|
+
this.acks.set(this.fnId, args.pop());
|
97
|
+
packet.id = this.fnId;
|
98
|
+
this.fnId += 1;
|
99
|
+
}
|
100
|
+
return this.sendPackage(packet);
|
101
|
+
};
|
102
|
+
send = (...args) => {
|
103
|
+
args.unshift('message');
|
104
|
+
return this.sendPackage({
|
105
|
+
type: PacketType.EVENT,
|
106
|
+
data: args,
|
107
|
+
});
|
108
|
+
};
|
109
|
+
sendPackage = (packet) => {
|
110
|
+
if (this.ws !== undefined && this.ws.readyState === 1 /* = OPEN */) {
|
111
|
+
const buffer = encodePacket(packet);
|
112
|
+
if (this.options.debug)
|
113
|
+
console.log(`(teckos:client) [${this.url}] Send packet: ${JSON.stringify(packet)}`);
|
114
|
+
this.ws.send(buffer);
|
115
|
+
return true;
|
116
|
+
}
|
117
|
+
return false;
|
118
|
+
};
|
119
|
+
handleMessage = (msg) => {
|
120
|
+
const packet = typeof msg.data === 'string'
|
121
|
+
? JSON.parse(msg.data)
|
122
|
+
: decodePacket(msg.data);
|
123
|
+
if (this.options.debug)
|
124
|
+
console.log(`(teckos:client) [${this.url}] Got packet: ${JSON.stringify(packet)}`);
|
125
|
+
if (packet.type === PacketType.EVENT) {
|
126
|
+
const event = packet.data[0];
|
127
|
+
const args = packet.data.slice(1);
|
128
|
+
if (event) {
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
130
|
+
this.listeners(event).forEach((listener) => listener(...args));
|
131
|
+
}
|
132
|
+
else {
|
133
|
+
throw new Error(`(teckos-client) [${this.url}] Got invalid event message: ${JSON.stringify(msg.data)}`);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
else if (packet.type === PacketType.ACK && packet.id !== undefined) {
|
137
|
+
// Call assigned function
|
138
|
+
const ack = this.acks.get(packet.id);
|
139
|
+
if (typeof ack === 'function') {
|
140
|
+
ack.apply(this, packet.data);
|
141
|
+
this.acks.delete(packet.id);
|
142
|
+
}
|
143
|
+
}
|
144
|
+
else {
|
145
|
+
throw new Error(`(teckos-client) [${this.url}] Got invalid message type: ${packet.type}`);
|
146
|
+
}
|
147
|
+
};
|
148
|
+
handleOpen = () => {
|
149
|
+
if (this.currentReconnectionAttempts > 0) {
|
150
|
+
// Reset reconnection settings to default
|
151
|
+
this.currentReconnectDelay = this.options.reconnectionDelay;
|
152
|
+
this.currentReconnectionAttempts = 0;
|
153
|
+
// Inform listeners
|
154
|
+
if (this.options.debug)
|
155
|
+
console.log(`(teckos:client) [${this.url}] Reconnected!`);
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
157
|
+
this.listeners('reconnect').forEach((listener) => listener());
|
158
|
+
}
|
159
|
+
// Inform listeners
|
160
|
+
if (this.options.debug)
|
161
|
+
console.log(`(teckos:client) [${this.url}] Connected!`);
|
162
|
+
this.listeners('connect').forEach((listener) => listener());
|
163
|
+
};
|
164
|
+
handleError = (error) => {
|
165
|
+
if (this.handlers && this.handlers.error) {
|
166
|
+
if (this.options.debug)
|
167
|
+
console.log(`(teckos:client) [${this.url}] Got error from server: ${JSON.stringify(error)}`);
|
168
|
+
this.handlers.error.forEach((listener) => listener(error));
|
169
|
+
}
|
170
|
+
};
|
171
|
+
handleClose = () => {
|
172
|
+
// Stop connection timeout
|
173
|
+
if (this.connectionTimeout) {
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
175
|
+
clearTimeout(this.connectionTimeout);
|
176
|
+
}
|
177
|
+
// Stop reconnection timeout
|
178
|
+
if (this.reconnectionTimeout) {
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
180
|
+
clearTimeout(this.reconnectionTimeout);
|
181
|
+
}
|
182
|
+
// Inform listeners
|
183
|
+
if (this.currentReconnectionAttempts > 0) {
|
184
|
+
if (this.options.debug)
|
185
|
+
console.log(`(teckos:client) [${this.url}] Reconnect #${this.currentReconnectionAttempts} failed!`);
|
186
|
+
this.listeners('reconnect_error').forEach((listener) => {
|
187
|
+
if (listener)
|
188
|
+
listener();
|
189
|
+
});
|
190
|
+
}
|
191
|
+
else {
|
192
|
+
if (this.options.debug)
|
193
|
+
console.log(`(teckos:client) [${this.url}] Disconnected!`);
|
194
|
+
this.listeners('disconnect').forEach((listener) => {
|
195
|
+
if (listener)
|
196
|
+
listener();
|
197
|
+
});
|
198
|
+
}
|
199
|
+
if (this.options.reconnection) {
|
200
|
+
// Apply reconnection logic
|
201
|
+
this.currentReconnectionAttempts += 1;
|
202
|
+
if (this.options.reconnectionAttempts === Infinity ||
|
203
|
+
this.currentReconnectionAttempts <= this.options.reconnectionAttempts) {
|
204
|
+
const timeout = Math.min(this.options.reconnectionDelayMax, this.currentReconnectDelay);
|
205
|
+
// Increase reconnection delay
|
206
|
+
this.currentReconnectDelay = Math.round(this.currentReconnectDelay +
|
207
|
+
this.currentReconnectDelay * this.options.randomizationFactor);
|
208
|
+
if (this.options.debug)
|
209
|
+
console.log(`(teckos:client) [${this.url}] Try reconnecting (${this.currentReconnectionAttempts}/${this.options.reconnectionAttempts}) in ${timeout}ms to ${this.url}...`);
|
210
|
+
this.reconnectionTimeout = setTimeout(() => {
|
211
|
+
this.reconnect();
|
212
|
+
}, timeout);
|
213
|
+
}
|
214
|
+
else {
|
215
|
+
if (this.options.debug)
|
216
|
+
console.log(`(teckos:client) [${this.url}] Reconnection maximum of ${this.options.reconnectionAttempts} reached`);
|
217
|
+
this.listeners('reconnect_failed').forEach((listener) => listener());
|
218
|
+
}
|
219
|
+
}
|
220
|
+
};
|
221
|
+
close = () => {
|
222
|
+
if (this.options.debug)
|
223
|
+
console.log(`(teckos:client) [${this.url}] Closing connection (client-side)`);
|
224
|
+
if (this.ws !== undefined) {
|
225
|
+
this.ws.onclose = () => { };
|
226
|
+
this.ws.close();
|
227
|
+
this.listeners('disconnect').forEach((listener) => listener());
|
228
|
+
}
|
229
|
+
};
|
230
|
+
disconnect = () => {
|
231
|
+
this.close();
|
232
|
+
};
|
233
|
+
}
|
234
|
+
export { TeckosClient };
|
235
|
+
//# sourceMappingURL=TeckosClient.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"TeckosClient.js","sourceRoot":"","sources":["TeckosClient.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,KAAK,mBAAmB,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAyC,eAAe,EAAU,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpG,MAAM,eAAe,GAAY;IAC7B,YAAY,EAAE,IAAI;IAClB,iBAAiB,EAAE,IAAI;IACvB,oBAAoB,EAAE,IAAI;IAC1B,oBAAoB,EAAE,QAAQ;IAC9B,mBAAmB,EAAE,GAAG;IACxB,OAAO,EAAE,IAAI;IACb,KAAK,EAAE,KAAK;CACf,CAAA;AAED,MAAM,YAAa,SAAQ,kBAA+B;IACnC,GAAG,CAAQ;IAEX,OAAO,CAAS;IAEnC,EAAE,CAA2C;IAEnC,qBAAqB,CAAQ;IAE7B,2BAA2B,GAAG,CAAC,CAAA;IAE/B,IAAI,GAA0C,IAAI,GAAG,EAAE,CAAA;IAEvD,IAAI,GAAG,CAAC,CAAA;IAER,iBAAiB,CAAiB;IAElC,mBAAmB,CAAiB;IAE9C,YAAY,GAAW,EAAE,OAAyB;QAC9C,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,OAAO,GAAG;YACX,GAAG,eAAe;YAClB,GAAG,OAAO;SACb,CAAA;QACD,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAA;QAC3D,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;IAClB,CAAC;IAES,aAAa,GAA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-1,11 +1,9 @@
|
|
1
|
-
|
2
|
-
import
|
1
|
+
/* eslint-disable no-console */
|
2
|
+
import * as IsomorphicWebSocket from 'isomorphic-ws'
|
3
3
|
import { decodePacket, encodePacket } from './util/Converter'
|
4
|
-
import { ConnectionState, OptionalOptions, Options, Packet, PacketType, SocketEvent } from './types'
|
5
4
|
import { ITeckosClient } from './ITeckosClient'
|
6
5
|
import { SocketEventEmitter } from './util/SocketEventEmitter'
|
7
|
-
|
8
|
-
const d = debug('teckos:client')
|
6
|
+
import { OptionalOptions, Options, SocketEvent, ConnectionState, Packet, PacketType } from './types'
|
9
7
|
|
10
8
|
const DEFAULT_OPTIONS: Options = {
|
11
9
|
reconnection: true,
|
@@ -22,7 +20,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
22
20
|
|
23
21
|
protected readonly options: Options
|
24
22
|
|
25
|
-
ws: WebSocket | undefined
|
23
|
+
ws: IsomorphicWebSocket.WebSocket | undefined
|
26
24
|
|
27
25
|
protected currentReconnectDelay: number
|
28
26
|
|
@@ -55,20 +53,21 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
55
53
|
}
|
56
54
|
}
|
57
55
|
|
58
|
-
public get webSocket(): WebSocket | undefined {
|
56
|
+
public get webSocket(): IsomorphicWebSocket.WebSocket | undefined {
|
59
57
|
return this.ws
|
60
58
|
}
|
61
59
|
|
62
60
|
public connect = (): void => {
|
63
|
-
if (this.options.debug)
|
61
|
+
if (this.options.debug) console.log(`(teckos:client) Connecting to ${this.url}...`)
|
64
62
|
|
65
63
|
// This will try to connect immediately
|
66
|
-
|
64
|
+
// eslint-disable-next-line new-cap
|
65
|
+
this.ws = new IsomorphicWebSocket.WebSocket(this.url)
|
67
66
|
// Attach handlers
|
68
67
|
this.attachHandler()
|
69
68
|
// Handle timeout
|
70
69
|
this.connectionTimeout = setTimeout(() => {
|
71
|
-
if (this.ws && this.ws.readyState ===
|
70
|
+
if (this.ws && this.ws.readyState === 0 /* = CONNECTING */) {
|
72
71
|
this.ws.close()
|
73
72
|
}
|
74
73
|
}, this.options.timeout)
|
@@ -82,13 +81,13 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
82
81
|
protected getConnectionState(): ConnectionState {
|
83
82
|
if (this.ws) {
|
84
83
|
switch (this.ws.readyState) {
|
85
|
-
case
|
86
|
-
return ConnectionState.CONNECTED
|
87
|
-
case WebSocket.CONNECTING:
|
84
|
+
case 0 /* = CONNECTING */:
|
88
85
|
return ConnectionState.CONNECTING
|
89
|
-
case
|
86
|
+
case 1 /* = OPEN */:
|
87
|
+
return ConnectionState.CONNECTED
|
88
|
+
case 2 /* = CLOSING */:
|
90
89
|
return ConnectionState.DISCONNECTING
|
91
|
-
default:
|
90
|
+
default: /* 3 = CLOSED */
|
92
91
|
return ConnectionState.DISCONNECTED
|
93
92
|
}
|
94
93
|
}
|
@@ -134,21 +133,23 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
134
133
|
}
|
135
134
|
|
136
135
|
public sendPackage = (packet: Packet): boolean => {
|
137
|
-
if (this.ws !== undefined && this.ws.readyState ===
|
136
|
+
if (this.ws !== undefined && this.ws.readyState === 1 /* = OPEN */) {
|
138
137
|
const buffer = encodePacket(packet)
|
139
|
-
if (this.options.debug)
|
138
|
+
if (this.options.debug)
|
139
|
+
console.log(`(teckos:client) [${this.url}] Send packet: ${JSON.stringify(packet)}`)
|
140
140
|
this.ws.send(buffer)
|
141
141
|
return true
|
142
142
|
}
|
143
143
|
return false
|
144
144
|
}
|
145
145
|
|
146
|
-
protected handleMessage = (msg:
|
146
|
+
protected handleMessage = (msg: IsomorphicWebSocket.MessageEvent): void => {
|
147
147
|
const packet =
|
148
148
|
typeof msg.data === 'string'
|
149
149
|
? (JSON.parse(msg.data) as Packet)
|
150
150
|
: decodePacket(msg.data as ArrayBuffer)
|
151
|
-
if (this.options.debug)
|
151
|
+
if (this.options.debug)
|
152
|
+
console.log(`(teckos:client) [${this.url}] Got packet: ${JSON.stringify(packet)}`)
|
152
153
|
if (packet.type === PacketType.EVENT) {
|
153
154
|
const event = packet.data[0] as SocketEvent
|
154
155
|
const args = packet.data.slice(1)
|
@@ -157,7 +158,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
157
158
|
this.listeners(event).forEach((listener) => listener(...args))
|
158
159
|
} else {
|
159
160
|
throw new Error(
|
160
|
-
`
|
161
|
+
`(teckos-client) [${this.url}] Got invalid event message: ${JSON.stringify(
|
161
162
|
msg.data
|
162
163
|
)}`
|
163
164
|
)
|
@@ -170,7 +171,9 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
170
171
|
this.acks.delete(packet.id)
|
171
172
|
}
|
172
173
|
} else {
|
173
|
-
throw new Error(
|
174
|
+
throw new Error(
|
175
|
+
`(teckos-client) [${this.url}] Got invalid message type: ${packet.type}`
|
176
|
+
)
|
174
177
|
}
|
175
178
|
}
|
176
179
|
|
@@ -181,19 +184,21 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
181
184
|
this.currentReconnectionAttempts = 0
|
182
185
|
|
183
186
|
// Inform listeners
|
184
|
-
if (this.options.debug)
|
187
|
+
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Reconnected!`)
|
185
188
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
186
189
|
this.listeners('reconnect').forEach((listener) => listener())
|
187
190
|
}
|
188
191
|
// Inform listeners
|
189
|
-
if (this.options.debug)
|
192
|
+
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Connected!`)
|
190
193
|
this.listeners('connect').forEach((listener) => listener())
|
191
194
|
}
|
192
195
|
|
193
|
-
protected handleError = (error:
|
196
|
+
protected handleError = (error: IsomorphicWebSocket.ErrorEvent): void => {
|
194
197
|
if (this.handlers && this.handlers.error) {
|
195
198
|
if (this.options.debug)
|
196
|
-
|
199
|
+
console.log(
|
200
|
+
`(teckos:client) [${this.url}] Got error from server: ${JSON.stringify(error)}`
|
201
|
+
)
|
197
202
|
this.handlers.error.forEach((listener) => listener(error))
|
198
203
|
}
|
199
204
|
}
|
@@ -213,12 +218,14 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
213
218
|
// Inform listeners
|
214
219
|
if (this.currentReconnectionAttempts > 0) {
|
215
220
|
if (this.options.debug)
|
216
|
-
|
221
|
+
console.log(
|
222
|
+
`(teckos:client) [${this.url}] Reconnect #${this.currentReconnectionAttempts} failed!`
|
223
|
+
)
|
217
224
|
this.listeners('reconnect_error').forEach((listener) => {
|
218
225
|
if (listener) listener()
|
219
226
|
})
|
220
227
|
} else {
|
221
|
-
if (this.options.debug)
|
228
|
+
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Disconnected!`)
|
222
229
|
this.listeners('disconnect').forEach((listener) => {
|
223
230
|
if (listener) listener()
|
224
231
|
})
|
@@ -243,16 +250,16 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
243
250
|
)
|
244
251
|
|
245
252
|
if (this.options.debug)
|
246
|
-
|
247
|
-
`[${this.url}] Try reconnecting (${this.currentReconnectionAttempts}/${this.options.reconnectionAttempts}) in ${timeout}ms to ${this.url}...`
|
253
|
+
console.log(
|
254
|
+
`(teckos:client) [${this.url}] Try reconnecting (${this.currentReconnectionAttempts}/${this.options.reconnectionAttempts}) in ${timeout}ms to ${this.url}...`
|
248
255
|
)
|
249
256
|
this.reconnectionTimeout = setTimeout(() => {
|
250
257
|
this.reconnect()
|
251
258
|
}, timeout)
|
252
259
|
} else {
|
253
260
|
if (this.options.debug)
|
254
|
-
|
255
|
-
`[${this.url}] Reconnection maximum of ${this.options.reconnectionAttempts} reached`
|
261
|
+
console.log(
|
262
|
+
`(teckos:client) [${this.url}] Reconnection maximum of ${this.options.reconnectionAttempts} reached`
|
256
263
|
)
|
257
264
|
this.listeners('reconnect_failed').forEach((listener) => listener())
|
258
265
|
}
|
@@ -260,7 +267,8 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
|
|
260
267
|
}
|
261
268
|
|
262
269
|
public close = (): void => {
|
263
|
-
if (this.options.debug)
|
270
|
+
if (this.options.debug)
|
271
|
+
console.log(`(teckos:client) [${this.url}] Closing connection (client-side)`)
|
264
272
|
if (this.ws !== undefined) {
|
265
273
|
this.ws.onclose = () => {}
|
266
274
|
this.ws.close()
|
@@ -0,0 +1,63 @@
|
|
1
|
+
/* eslint-disable no-console */
|
2
|
+
import * as IsomorphicWebSocket from 'isomorphic-ws';
|
3
|
+
import { TeckosClient } from './TeckosClient';
|
4
|
+
import { ConnectionState } from './types';
|
5
|
+
class TeckosClientWithJWT extends TeckosClient {
|
6
|
+
token;
|
7
|
+
initialData;
|
8
|
+
receivedReady = false;
|
9
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
10
|
+
constructor(url, options, token, initialData) {
|
11
|
+
super(url, options);
|
12
|
+
this.token = token;
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
14
|
+
this.initialData = initialData;
|
15
|
+
this.on('disconnect', () => {
|
16
|
+
this.receivedReady = false;
|
17
|
+
});
|
18
|
+
}
|
19
|
+
getConnectionState() {
|
20
|
+
if (this.ws) {
|
21
|
+
switch (this.ws.readyState) {
|
22
|
+
case IsomorphicWebSocket.WebSocket.OPEN:
|
23
|
+
if (this.receivedReady) {
|
24
|
+
return ConnectionState.CONNECTED;
|
25
|
+
}
|
26
|
+
return ConnectionState.CONNECTING;
|
27
|
+
case IsomorphicWebSocket.WebSocket.CONNECTING:
|
28
|
+
return ConnectionState.CONNECTING;
|
29
|
+
case IsomorphicWebSocket.WebSocket.CLOSING:
|
30
|
+
return ConnectionState.DISCONNECTING;
|
31
|
+
default:
|
32
|
+
return ConnectionState.DISCONNECTED;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
return ConnectionState.DISCONNECTED;
|
36
|
+
}
|
37
|
+
handleReadyEvent = () => {
|
38
|
+
if (this.options.debug)
|
39
|
+
console.log(`(teckos:client) [${this.url}] Connected!`);
|
40
|
+
this.receivedReady = true;
|
41
|
+
if (this.currentReconnectionAttempts > 0) {
|
42
|
+
if (this.options.debug)
|
43
|
+
console.log(`(teckos:client) [${this.url}] Reconnected!`);
|
44
|
+
this.listeners('reconnect').forEach((listener) => listener());
|
45
|
+
// Reset reconnection settings to default
|
46
|
+
this.currentReconnectDelay = this.options.reconnectionDelay;
|
47
|
+
this.currentReconnectionAttempts = 0;
|
48
|
+
}
|
49
|
+
this.listeners('connect').forEach((listener) => listener());
|
50
|
+
};
|
51
|
+
handleOpen = () => {
|
52
|
+
this.receivedReady = false;
|
53
|
+
this.once('ready', this.handleReadyEvent);
|
54
|
+
if (this.options.debug)
|
55
|
+
console.log(`(teckos:client) Connection opened, sending token now`);
|
56
|
+
this.emit('token', {
|
57
|
+
token: this.token,
|
58
|
+
...this.initialData,
|
59
|
+
});
|
60
|
+
};
|
61
|
+
}
|
62
|
+
export { TeckosClientWithJWT };
|
63
|
+
//# sourceMappingURL=TeckosClientWithJWT.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"TeckosClientWithJWT.js","sourceRoot":"","sources":["TeckosClientWithJWT.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,KAAK,mBAAmB,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAmB,eAAe,EAAE,MAAM,SAAS,CAAA;AAE1D,MAAM,mBAAoB,SAAQ,YAAY;IACvB,KAAK,CAAQ;IAEb,WAAW,CAAK;IAEzB,aAAa,GAAG,KAAK,CAAA;IAE/B,6EAA6E;IAC7E,YAAY,GAAW,EAAE,OAAwB,EAAE,KAAa,EAAE,WAAiB;QAC/E,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QACnB,IAAI,CAAC,KAAK,GAAG,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,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAA;YAC3D,IAAI,CAAC,2BAA2B,GAAG,CAAC,CAAA;SACvC;QACD,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC/D,CAAC,CAAA;IAES,UAAU,GAAG,GAAS,EAAE;QAC9B,IAAI,CAAC,aAAa,GAAG,KAAK,CAAA;QAC1B,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAA;QACzC,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK;YAAE,OAAO,CAAC,GAAG,CAAC,sDAAsD,CAAC,CAAA;QAC3F,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;YACf,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,GAAG,IAAI,CAAC,WAAW;SACtB,CAAC,CAAA;IACN,CAAC,CAAA;CACJ;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAA"}
|
@@ -1,10 +1,8 @@
|
|
1
|
-
|
2
|
-
import
|
1
|
+
/* eslint-disable no-console */
|
2
|
+
import * as IsomorphicWebSocket from 'isomorphic-ws'
|
3
3
|
import { TeckosClient } from './TeckosClient'
|
4
4
|
import { OptionalOptions, ConnectionState } from './types'
|
5
5
|
|
6
|
-
const d = debug('teckos:client')
|
7
|
-
|
8
6
|
class TeckosClientWithJWT extends TeckosClient {
|
9
7
|
protected readonly token: string
|
10
8
|
|
@@ -26,14 +24,14 @@ class TeckosClientWithJWT extends TeckosClient {
|
|
26
24
|
protected getConnectionState(): ConnectionState {
|
27
25
|
if (this.ws) {
|
28
26
|
switch (this.ws.readyState) {
|
29
|
-
case WebSocket.OPEN:
|
27
|
+
case IsomorphicWebSocket.WebSocket.OPEN:
|
30
28
|
if (this.receivedReady) {
|
31
29
|
return ConnectionState.CONNECTED
|
32
30
|
}
|
33
31
|
return ConnectionState.CONNECTING
|
34
|
-
case WebSocket.CONNECTING:
|
32
|
+
case IsomorphicWebSocket.WebSocket.CONNECTING:
|
35
33
|
return ConnectionState.CONNECTING
|
36
|
-
case WebSocket.CLOSING:
|
34
|
+
case IsomorphicWebSocket.WebSocket.CLOSING:
|
37
35
|
return ConnectionState.DISCONNECTING
|
38
36
|
default:
|
39
37
|
return ConnectionState.DISCONNECTED
|
@@ -43,10 +41,10 @@ class TeckosClientWithJWT extends TeckosClient {
|
|
43
41
|
}
|
44
42
|
|
45
43
|
protected handleReadyEvent = (): void => {
|
46
|
-
if (this.options.debug)
|
44
|
+
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Connected!`)
|
47
45
|
this.receivedReady = true
|
48
46
|
if (this.currentReconnectionAttempts > 0) {
|
49
|
-
if (this.options.debug)
|
47
|
+
if (this.options.debug) console.log(`(teckos:client) [${this.url}] Reconnected!`)
|
50
48
|
this.listeners('reconnect').forEach((listener) => listener())
|
51
49
|
// Reset reconnection settings to default
|
52
50
|
this.currentReconnectDelay = this.options.reconnectionDelay
|
@@ -58,7 +56,7 @@ class TeckosClientWithJWT extends TeckosClient {
|
|
58
56
|
protected handleOpen = (): void => {
|
59
57
|
this.receivedReady = false
|
60
58
|
this.once('ready', this.handleReadyEvent)
|
61
|
-
if (this.options.debug)
|
59
|
+
if (this.options.debug) console.log(`(teckos:client) Connection opened, sending token now`)
|
62
60
|
this.emit('token', {
|
63
61
|
token: this.token,
|
64
62
|
...this.initialData,
|
package/src/index.js
ADDED
package/src/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAG7C;;GAEG;AACH,cAAc,SAAS,CAAA;AAGvB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,CAAA"}
|
package/src/index.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import { TeckosClientWithJWT } from './TeckosClientWithJWT'
|
2
2
|
import { TeckosClient } from './TeckosClient'
|
3
3
|
import { ITeckosClient } from './ITeckosClient'
|
4
|
-
import { ConnectionState, OptionalOptions, Options, Packet, PacketType, SocketEvent } from './types'
|
5
4
|
|
6
5
|
/**
|
7
6
|
* Expose all types
|
8
7
|
*/
|
9
|
-
export
|
8
|
+
export * from './types'
|
9
|
+
export type { ITeckosClient }
|
10
10
|
|
11
|
-
export {
|
11
|
+
export { TeckosClient, TeckosClientWithJWT }
|
@@ -0,0 +1,9 @@
|
|
1
|
+
var ConnectionState;
|
2
|
+
(function (ConnectionState) {
|
3
|
+
ConnectionState["DISCONNECTED"] = "disconnected";
|
4
|
+
ConnectionState["CONNECTING"] = "connecting";
|
5
|
+
ConnectionState["CONNECTED"] = "connected";
|
6
|
+
ConnectionState["DISCONNECTING"] = "disconnecting";
|
7
|
+
})(ConnectionState || (ConnectionState = {}));
|
8
|
+
export { ConnectionState };
|
9
|
+
//# sourceMappingURL=ConnectionState.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ConnectionState.js","sourceRoot":"","sources":["ConnectionState.ts"],"names":[],"mappings":"AAAA,IAAK,eAKJ;AALD,WAAK,eAAe;IAChB,gDAA6B,CAAA;IAC7B,4CAAyB,CAAA;IACzB,0CAAuB,CAAA;IACvB,kDAA+B,CAAA;AACnC,CAAC,EALI,eAAe,KAAf,eAAe,QAKnB;AACD,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Options.js","sourceRoot":"","sources":["Options.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Packet.js","sourceRoot":"","sources":["Packet.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"PacketType.js","sourceRoot":"","sources":["PacketType.ts"],"names":[],"mappings":"AAAA,IAAK,UAGJ;AAHD,WAAK,UAAU;IACX,6CAAK,CAAA;IACL,yCAAG,CAAA;AACP,CAAC,EAHI,UAAU,KAAV,UAAU,QAGd;AACD,OAAO,EAAE,UAAU,EAAE,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"SocketEvent.js","sourceRoot":"","sources":["SocketEvent.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAGzC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAEnD,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,CAAA"}
|