laravel-echo-api-gateway 0.1.2 → 0.5.2
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/.github/workflows/ci.yml +10 -7
- package/.github/workflows/release.yml +2 -2
- package/README.md +19 -1
- package/dist/js-src/Websocket.d.ts +6 -0
- package/dist/laravel-echo-api-gateway.common.js +230 -67
- package/dist/laravel-echo-api-gateway.iife.js +230 -67
- package/dist/laravel-echo-api-gateway.js +230 -67
- package/js-src/Channel.ts +8 -5
- package/js-src/Websocket.ts +92 -44
- package/js-tests/Connector.test.ts +57 -5
- package/package.json +1 -1
- package/phpstan.neon.dist +2 -0
|
@@ -17,20 +17,44 @@ function _defineProperties(target, props) {
|
|
|
17
17
|
function _createClass(Constructor, protoProps, staticProps) {
|
|
18
18
|
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
19
19
|
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
20
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
21
|
+
writable: false
|
|
22
|
+
});
|
|
20
23
|
return Constructor;
|
|
21
24
|
}
|
|
22
25
|
|
|
26
|
+
function _extends() {
|
|
27
|
+
_extends = Object.assign || function (target) {
|
|
28
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
29
|
+
var source = arguments[i];
|
|
30
|
+
|
|
31
|
+
for (var key in source) {
|
|
32
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
33
|
+
target[key] = source[key];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return target;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
return _extends.apply(this, arguments);
|
|
42
|
+
}
|
|
43
|
+
|
|
23
44
|
function _inherits(subClass, superClass) {
|
|
24
45
|
if (typeof superClass !== "function" && superClass !== null) {
|
|
25
46
|
throw new TypeError("Super expression must either be null or a function");
|
|
26
47
|
}
|
|
27
48
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
49
|
+
Object.defineProperty(subClass, "prototype", {
|
|
50
|
+
value: Object.create(superClass && superClass.prototype, {
|
|
51
|
+
constructor: {
|
|
52
|
+
value: subClass,
|
|
53
|
+
writable: true,
|
|
54
|
+
configurable: true
|
|
55
|
+
}
|
|
56
|
+
}),
|
|
57
|
+
writable: false
|
|
34
58
|
});
|
|
35
59
|
if (superClass) _setPrototypeOf(subClass, superClass);
|
|
36
60
|
}
|
|
@@ -57,7 +81,7 @@ function _isNativeReflectConstruct() {
|
|
|
57
81
|
if (typeof Proxy === "function") return true;
|
|
58
82
|
|
|
59
83
|
try {
|
|
60
|
-
|
|
84
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
61
85
|
return true;
|
|
62
86
|
} catch (e) {
|
|
63
87
|
return false;
|
|
@@ -75,6 +99,8 @@ function _assertThisInitialized(self) {
|
|
|
75
99
|
function _possibleConstructorReturn(self, call) {
|
|
76
100
|
if (call && (typeof call === "object" || typeof call === "function")) {
|
|
77
101
|
return call;
|
|
102
|
+
} else if (call !== void 0) {
|
|
103
|
+
throw new TypeError("Derived constructors may only return object or undefined");
|
|
78
104
|
}
|
|
79
105
|
|
|
80
106
|
return _assertThisInitialized(self);
|
|
@@ -99,6 +125,80 @@ function _createSuper(Derived) {
|
|
|
99
125
|
};
|
|
100
126
|
}
|
|
101
127
|
|
|
128
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
129
|
+
if (!o) return;
|
|
130
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
131
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
132
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
133
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
134
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function _arrayLikeToArray(arr, len) {
|
|
138
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
139
|
+
|
|
140
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
141
|
+
|
|
142
|
+
return arr2;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
146
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
147
|
+
|
|
148
|
+
if (!it) {
|
|
149
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
150
|
+
if (it) o = it;
|
|
151
|
+
var i = 0;
|
|
152
|
+
|
|
153
|
+
var F = function () {};
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
s: F,
|
|
157
|
+
n: function () {
|
|
158
|
+
if (i >= o.length) return {
|
|
159
|
+
done: true
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
done: false,
|
|
163
|
+
value: o[i++]
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
e: function (e) {
|
|
167
|
+
throw e;
|
|
168
|
+
},
|
|
169
|
+
f: F
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
var normalCompletion = true,
|
|
177
|
+
didErr = false,
|
|
178
|
+
err;
|
|
179
|
+
return {
|
|
180
|
+
s: function () {
|
|
181
|
+
it = it.call(o);
|
|
182
|
+
},
|
|
183
|
+
n: function () {
|
|
184
|
+
var step = it.next();
|
|
185
|
+
normalCompletion = step.done;
|
|
186
|
+
return step;
|
|
187
|
+
},
|
|
188
|
+
e: function (e) {
|
|
189
|
+
didErr = true;
|
|
190
|
+
err = e;
|
|
191
|
+
},
|
|
192
|
+
f: function () {
|
|
193
|
+
try {
|
|
194
|
+
if (!normalCompletion && it.return != null) it.return();
|
|
195
|
+
} finally {
|
|
196
|
+
if (didErr) throw err;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
102
202
|
class Connector {
|
|
103
203
|
/**
|
|
104
204
|
* Create a new class instance.
|
|
@@ -153,83 +253,136 @@ class Connector {
|
|
|
153
253
|
|
|
154
254
|
var Websocket = /*#__PURE__*/function () {
|
|
155
255
|
function Websocket(options) {
|
|
156
|
-
var _this = this;
|
|
157
|
-
|
|
158
256
|
_classCallCheck(this, Websocket);
|
|
159
257
|
|
|
160
258
|
this.buffer = [];
|
|
161
259
|
this.listeners = {};
|
|
162
260
|
this.internalListeners = {};
|
|
163
261
|
this.channelBacklog = [];
|
|
262
|
+
this.closing = false;
|
|
263
|
+
this.hasConnected = false;
|
|
164
264
|
this.options = options;
|
|
165
|
-
this.
|
|
265
|
+
this.connect(this.options.host);
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
166
268
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
269
|
+
_createClass(Websocket, [{
|
|
270
|
+
key: "connect",
|
|
271
|
+
value: function connect(host) {
|
|
272
|
+
var _this = this;
|
|
170
273
|
|
|
171
|
-
|
|
274
|
+
this.options.debug && console.log('Trying to connect...');
|
|
275
|
+
this.websocket = new WebSocket(host);
|
|
172
276
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
277
|
+
this.websocket.onerror = function () {
|
|
278
|
+
if (!_this.hasConnected) {
|
|
279
|
+
setTimeout(function () {
|
|
280
|
+
_this.socketId = undefined;
|
|
176
281
|
|
|
177
|
-
|
|
178
|
-
|
|
282
|
+
_this.connect(host);
|
|
283
|
+
}, 3000);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
179
286
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
287
|
+
this.websocket.onopen = function () {
|
|
288
|
+
_this.options.debug && console.log('Connected !');
|
|
289
|
+
_this.hasConnected = true;
|
|
183
290
|
|
|
184
|
-
|
|
185
|
-
|
|
291
|
+
_this.send({
|
|
292
|
+
event: 'whoami'
|
|
293
|
+
});
|
|
186
294
|
|
|
187
|
-
|
|
188
|
-
_this.
|
|
189
|
-
}
|
|
295
|
+
while (_this.buffer.length) {
|
|
296
|
+
var message = _this.buffer[0];
|
|
190
297
|
|
|
191
|
-
|
|
192
|
-
}
|
|
298
|
+
_this.send(message);
|
|
193
299
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
};
|
|
300
|
+
_this.buffer.splice(0, 1);
|
|
301
|
+
} // Register events only once connected, or they won't be registered if connection failed/lost
|
|
198
302
|
|
|
199
|
-
this.on('whoami', function (_ref) {
|
|
200
|
-
var socketId = _ref.socket_id;
|
|
201
|
-
_this.socketId = socketId;
|
|
202
|
-
console.log("just set socketId to ".concat(socketId));
|
|
203
303
|
|
|
204
|
-
|
|
205
|
-
|
|
304
|
+
_this.websocket.onmessage = function (messageEvent) {
|
|
305
|
+
var message = _this.parseMessage(messageEvent.data);
|
|
206
306
|
|
|
207
|
-
|
|
307
|
+
_this.options.debug && console.log('onmessage', messageEvent.data);
|
|
208
308
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
this.send({
|
|
213
|
-
event: 'whoami'
|
|
214
|
-
}); // send ping every 60 seconds to keep connection alive
|
|
309
|
+
if (!message) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
215
312
|
|
|
216
|
-
|
|
217
|
-
|
|
313
|
+
if (message.channel) {
|
|
314
|
+
_this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
|
|
218
315
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}, 60 * 1000);
|
|
223
|
-
return this;
|
|
224
|
-
}
|
|
316
|
+
if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
|
|
317
|
+
_this.listeners[message.channel][message.event](message.data);
|
|
318
|
+
}
|
|
225
319
|
|
|
226
|
-
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (_this.internalListeners[message.event]) {
|
|
324
|
+
_this.internalListeners[message.event](message.data);
|
|
325
|
+
}
|
|
326
|
+
}; // send ping every 60 seconds to keep connection alive
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
_this.pingInterval = setInterval(function () {
|
|
330
|
+
if (_this.websocket.readyState === _this.websocket.OPEN) {
|
|
331
|
+
_this.options.debug && console.log('Sending ping');
|
|
332
|
+
|
|
333
|
+
_this.send({
|
|
334
|
+
event: 'ping'
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}, 60 * 1000);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
this.websocket.onclose = function () {
|
|
341
|
+
_this.options.debug && console.info('Connection closed.');
|
|
342
|
+
|
|
343
|
+
if (_this.closing) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
_this.hasConnected = false;
|
|
348
|
+
_this.options.debug && console.info('Connection lost, reconnecting...');
|
|
349
|
+
setTimeout(function () {
|
|
350
|
+
_this.socketId = undefined;
|
|
351
|
+
|
|
352
|
+
_this.connect(host);
|
|
353
|
+
}, 1000);
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
this.on('whoami', function (_ref) {
|
|
357
|
+
var socketId = _ref.socket_id;
|
|
358
|
+
_this.socketId = socketId;
|
|
359
|
+
_this.options.debug && console.log("just set socketId to ".concat(socketId)); // Handle the backlog and don't empty it, we'll need it if we lose connection
|
|
360
|
+
|
|
361
|
+
var channel;
|
|
362
|
+
|
|
363
|
+
var _iterator = _createForOfIteratorHelper(_this.channelBacklog),
|
|
364
|
+
_step;
|
|
365
|
+
|
|
366
|
+
try {
|
|
367
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
368
|
+
channel = _step.value;
|
|
369
|
+
|
|
370
|
+
_this.actuallySubscribe(channel);
|
|
371
|
+
}
|
|
372
|
+
} catch (err) {
|
|
373
|
+
_iterator.e(err);
|
|
374
|
+
} finally {
|
|
375
|
+
_iterator.f();
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}, {
|
|
227
380
|
key: "parseMessage",
|
|
228
381
|
value: function parseMessage(body) {
|
|
229
382
|
try {
|
|
230
383
|
return JSON.parse(body);
|
|
231
384
|
} catch (error) {
|
|
232
|
-
console.error(error);
|
|
385
|
+
this.options.debug && console.error(error);
|
|
233
386
|
return undefined;
|
|
234
387
|
}
|
|
235
388
|
}
|
|
@@ -256,6 +409,7 @@ var Websocket = /*#__PURE__*/function () {
|
|
|
256
409
|
}, {
|
|
257
410
|
key: "close",
|
|
258
411
|
value: function close() {
|
|
412
|
+
this.closing = true;
|
|
259
413
|
this.internalListeners = {};
|
|
260
414
|
clearInterval(this.pingInterval);
|
|
261
415
|
this.pingInterval = undefined;
|
|
@@ -276,26 +430,32 @@ var Websocket = /*#__PURE__*/function () {
|
|
|
276
430
|
var _this2 = this;
|
|
277
431
|
|
|
278
432
|
if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
|
|
279
|
-
console.log("Sending auth request for channel ".concat(channel.name));
|
|
433
|
+
this.options.debug && console.log("Sending auth request for channel ".concat(channel.name));
|
|
434
|
+
|
|
435
|
+
if (this.options.bearerToken) {
|
|
436
|
+
this.options.auth.headers['Authorization'] = 'Bearer ' + this.options.bearerToken;
|
|
437
|
+
}
|
|
438
|
+
|
|
280
439
|
axios.post(this.options.authEndpoint, {
|
|
281
440
|
socket_id: this.getSocketId(),
|
|
282
441
|
channel_name: channel.name
|
|
442
|
+
}, {
|
|
443
|
+
headers: this.options.auth.headers || {}
|
|
283
444
|
}).then(function (response) {
|
|
284
|
-
console.log("Subscribing to channels ".concat(channel.name));
|
|
445
|
+
_this2.options.debug && console.log("Subscribing to channels ".concat(channel.name));
|
|
285
446
|
|
|
286
447
|
_this2.send({
|
|
287
448
|
event: 'subscribe',
|
|
288
|
-
data: {
|
|
289
|
-
channel: channel.name
|
|
290
|
-
|
|
291
|
-
}
|
|
449
|
+
data: _extends({
|
|
450
|
+
channel: channel.name
|
|
451
|
+
}, response.data)
|
|
292
452
|
});
|
|
293
453
|
})["catch"](function (error) {
|
|
294
|
-
console.log("Auth request for channel ".concat(channel.name, " failed"));
|
|
295
|
-
console.error(error);
|
|
454
|
+
_this2.options.debug && console.log("Auth request for channel ".concat(channel.name, " failed"));
|
|
455
|
+
_this2.options.debug && console.error(error);
|
|
296
456
|
});
|
|
297
457
|
} else {
|
|
298
|
-
console.log("Subscribing to channels ".concat(channel.name));
|
|
458
|
+
this.options.debug && console.log("Subscribing to channels ".concat(channel.name));
|
|
299
459
|
this.send({
|
|
300
460
|
event: 'subscribe',
|
|
301
461
|
data: {
|
|
@@ -504,9 +664,12 @@ var Channel$1 = /*#__PURE__*/function (_BaseChannel) {
|
|
|
504
664
|
}, {
|
|
505
665
|
key: "whisper",
|
|
506
666
|
value: function whisper(event, data) {
|
|
667
|
+
var channel = this.name;
|
|
668
|
+
var formattedEvent = "client-" + event;
|
|
507
669
|
this.socket.send({
|
|
508
|
-
event:
|
|
509
|
-
data: data
|
|
670
|
+
"event": formattedEvent,
|
|
671
|
+
data: data,
|
|
672
|
+
channel: channel
|
|
510
673
|
});
|
|
511
674
|
return this;
|
|
512
675
|
}
|
package/js-src/Channel.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {EventFormatter} from 'laravel-echo/src/util';
|
|
2
|
-
import {Channel as BaseChannel} from 'laravel-echo/src/channel/channel';
|
|
3
|
-
import {PresenceChannel} from "laravel-echo/src/channel";
|
|
4
|
-
import {Websocket} from "./Websocket";
|
|
1
|
+
import { EventFormatter } from 'laravel-echo/src/util';
|
|
2
|
+
import { Channel as BaseChannel } from 'laravel-echo/src/channel/channel';
|
|
3
|
+
import { PresenceChannel } from "laravel-echo/src/channel";
|
|
4
|
+
import { Websocket } from "./Websocket";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* This class represents a Pusher channel.
|
|
@@ -105,9 +105,12 @@ export class Channel extends BaseChannel implements PresenceChannel {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
whisper(event: string, data: object): Channel {
|
|
108
|
+
let channel = this.name;
|
|
109
|
+
let formattedEvent = "client-" + event;
|
|
108
110
|
this.socket.send({
|
|
109
|
-
event,
|
|
111
|
+
"event": formattedEvent,
|
|
110
112
|
data,
|
|
113
|
+
channel,
|
|
111
114
|
})
|
|
112
115
|
|
|
113
116
|
return this;
|
package/js-src/Websocket.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { AxiosResponse } from "axios";
|
|
2
|
+
import { Channel } from "./Channel";
|
|
3
|
+
|
|
4
|
+
export type Options = { authEndpoint: string, host: string, bearerToken: string, auth: any, debug: boolean };
|
|
3
5
|
|
|
4
|
-
export type Options = { authEndpoint: string, host: string };
|
|
5
6
|
export type MessageBody = { event: string, channel?: string, data: object };
|
|
6
7
|
|
|
7
8
|
export class Websocket {
|
|
@@ -19,14 +20,32 @@ export class Websocket {
|
|
|
19
20
|
|
|
20
21
|
private socketId: string;
|
|
21
22
|
|
|
23
|
+
private closing = false;
|
|
24
|
+
private hasConnected = false;
|
|
25
|
+
|
|
22
26
|
private pingInterval: NodeJS.Timeout;
|
|
23
27
|
|
|
24
|
-
|
|
25
|
-
this.options
|
|
28
|
+
private connect(host: string): void {
|
|
29
|
+
this.options.debug && console.log('Trying to connect...');
|
|
26
30
|
|
|
27
|
-
this.websocket = new WebSocket(
|
|
31
|
+
this.websocket = new WebSocket(host)
|
|
32
|
+
|
|
33
|
+
this.websocket.onerror = () => {
|
|
34
|
+
if (!this.hasConnected) {
|
|
35
|
+
setTimeout(() => {
|
|
36
|
+
this.socketId = undefined
|
|
37
|
+
this.connect(host)
|
|
38
|
+
}, 3000);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
28
41
|
|
|
29
42
|
this.websocket.onopen = () => {
|
|
43
|
+
this.options.debug && console.log('Connected !');
|
|
44
|
+
this.hasConnected = true;
|
|
45
|
+
this.send({
|
|
46
|
+
event: 'whoami',
|
|
47
|
+
})
|
|
48
|
+
|
|
30
49
|
while (this.buffer.length) {
|
|
31
50
|
const message = this.buffer[0]
|
|
32
51
|
|
|
@@ -34,56 +53,78 @@ export class Websocket {
|
|
|
34
53
|
|
|
35
54
|
this.buffer.splice(0, 1)
|
|
36
55
|
}
|
|
37
|
-
}
|
|
38
56
|
|
|
39
|
-
|
|
40
|
-
const message = this.parseMessage(messageEvent.data)
|
|
57
|
+
// Register events only once connected, or they won't be registered if connection failed/lost
|
|
41
58
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
59
|
+
this.websocket.onmessage = (messageEvent: MessageEvent) => {
|
|
60
|
+
const message = this.parseMessage(messageEvent.data)
|
|
61
|
+
this.options.debug && console.log('onmessage', messageEvent.data)
|
|
62
|
+
|
|
63
|
+
if (!message) {
|
|
64
|
+
return
|
|
65
|
+
}
|
|
45
66
|
|
|
46
|
-
|
|
47
|
-
|
|
67
|
+
if (message.channel) {
|
|
68
|
+
this.options.debug && console.log(`Received event ${message.event} on channel ${message.channel}`)
|
|
48
69
|
|
|
49
|
-
|
|
50
|
-
|
|
70
|
+
if (this.listeners[message.channel] && this.listeners[message.channel][message.event]) {
|
|
71
|
+
this.listeners[message.channel][message.event](message.data)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return
|
|
51
75
|
}
|
|
52
76
|
|
|
53
|
-
|
|
77
|
+
if (this.internalListeners[message.event]) {
|
|
78
|
+
this.internalListeners[message.event](message.data)
|
|
79
|
+
}
|
|
54
80
|
}
|
|
55
81
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
82
|
+
|
|
83
|
+
// send ping every 60 seconds to keep connection alive
|
|
84
|
+
this.pingInterval = setInterval(() => {
|
|
85
|
+
if (this.websocket.readyState === this.websocket.OPEN) {
|
|
86
|
+
this.options.debug && console.log('Sending ping')
|
|
87
|
+
this.send({
|
|
88
|
+
event: 'ping',
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
}, 60 * 1000)
|
|
59
92
|
}
|
|
60
93
|
|
|
94
|
+
|
|
95
|
+
this.websocket.onclose = () => {
|
|
96
|
+
this.options.debug && console.info('Connection closed.');
|
|
97
|
+
if (this.closing){
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.hasConnected = false
|
|
101
|
+
this.options.debug && console.info('Connection lost, reconnecting...');
|
|
102
|
+
setTimeout(() => {
|
|
103
|
+
this.socketId = undefined
|
|
104
|
+
this.connect(host)
|
|
105
|
+
}, 1000);
|
|
106
|
+
};
|
|
107
|
+
|
|
61
108
|
this.on('whoami', ({ socket_id: socketId }) => {
|
|
62
109
|
this.socketId = socketId
|
|
63
110
|
|
|
64
|
-
console.log(`just set socketId to ${socketId}`)
|
|
65
|
-
|
|
66
|
-
while (this.channelBacklog.length) {
|
|
67
|
-
const channel = this.channelBacklog[0]
|
|
111
|
+
this.options.debug && console.log(`just set socketId to ${socketId}`)
|
|
68
112
|
|
|
113
|
+
// Handle the backlog and don't empty it, we'll need it if we lose connection
|
|
114
|
+
let channel: Channel;
|
|
115
|
+
for(channel of this.channelBacklog){
|
|
69
116
|
this.actuallySubscribe(channel)
|
|
70
|
-
|
|
71
|
-
this.channelBacklog.splice(0, 1)
|
|
72
117
|
}
|
|
73
|
-
})
|
|
74
118
|
|
|
75
|
-
this.send({
|
|
76
|
-
event: 'whoami',
|
|
77
119
|
})
|
|
78
120
|
|
|
79
|
-
// send ping every 60 seconds to keep connection alive
|
|
80
|
-
this.pingInterval = setInterval(() => {
|
|
81
|
-
console.log('Sending ping')
|
|
82
121
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
constructor(options: Options) {
|
|
125
|
+
this.options = options;
|
|
126
|
+
|
|
127
|
+
this.connect(this.options.host);
|
|
87
128
|
|
|
88
129
|
return this
|
|
89
130
|
}
|
|
@@ -92,7 +133,7 @@ export class Websocket {
|
|
|
92
133
|
try {
|
|
93
134
|
return JSON.parse(body)
|
|
94
135
|
} catch (error) {
|
|
95
|
-
console.error(error)
|
|
136
|
+
this.options.debug && console.error(error)
|
|
96
137
|
|
|
97
138
|
return undefined
|
|
98
139
|
}
|
|
@@ -115,7 +156,8 @@ export class Websocket {
|
|
|
115
156
|
this.buffer.push(message)
|
|
116
157
|
}
|
|
117
158
|
|
|
118
|
-
close
|
|
159
|
+
close(): void {
|
|
160
|
+
this.closing = true
|
|
119
161
|
this.internalListeners = {}
|
|
120
162
|
|
|
121
163
|
clearInterval(this.pingInterval)
|
|
@@ -134,27 +176,33 @@ export class Websocket {
|
|
|
134
176
|
|
|
135
177
|
private actuallySubscribe(channel: Channel): void {
|
|
136
178
|
if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
|
|
137
|
-
console.log(`Sending auth request for channel ${channel.name}`)
|
|
179
|
+
this.options.debug && console.log(`Sending auth request for channel ${channel.name}`)
|
|
180
|
+
|
|
181
|
+
if (this.options.bearerToken) {
|
|
182
|
+
this.options.auth.headers['Authorization'] = 'Bearer ' + this.options.bearerToken;
|
|
183
|
+
}
|
|
138
184
|
|
|
139
185
|
axios.post(this.options.authEndpoint, {
|
|
140
186
|
socket_id: this.getSocketId(),
|
|
141
187
|
channel_name: channel.name,
|
|
188
|
+
}, {
|
|
189
|
+
headers: this.options.auth.headers || {}
|
|
142
190
|
}).then((response: AxiosResponse) => {
|
|
143
|
-
console.log(`Subscribing to channels ${channel.name}`)
|
|
191
|
+
this.options.debug && console.log(`Subscribing to channels ${channel.name}`)
|
|
144
192
|
|
|
145
193
|
this.send({
|
|
146
194
|
event: 'subscribe',
|
|
147
195
|
data: {
|
|
148
196
|
channel: channel.name,
|
|
149
|
-
|
|
197
|
+
...response.data
|
|
150
198
|
},
|
|
151
199
|
})
|
|
152
200
|
}).catch((error) => {
|
|
153
|
-
console.log(`Auth request for channel ${channel.name} failed`)
|
|
154
|
-
console.error(error)
|
|
201
|
+
this.options.debug && console.log(`Auth request for channel ${channel.name} failed`)
|
|
202
|
+
this.options.debug && console.error(error)
|
|
155
203
|
})
|
|
156
204
|
} else {
|
|
157
|
-
console.log(`Subscribing to channels ${channel.name}`)
|
|
205
|
+
this.options.debug && console.log(`Subscribing to channels ${channel.name}`)
|
|
158
206
|
|
|
159
207
|
this.send({
|
|
160
208
|
event: 'subscribe',
|