laravel-echo-api-gateway 0.5.1 → 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.
@@ -20,6 +20,7 @@ export declare class Websocket {
20
20
  private channelBacklog;
21
21
  private socketId;
22
22
  private closing;
23
+ private hasConnected;
23
24
  private pingInterval;
24
25
  private connect;
25
26
  constructor(options: Options);
@@ -129,6 +129,80 @@ function _createSuper(Derived) {
129
129
  };
130
130
  }
131
131
 
132
+ function _unsupportedIterableToArray(o, minLen) {
133
+ if (!o) return;
134
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
135
+ var n = Object.prototype.toString.call(o).slice(8, -1);
136
+ if (n === "Object" && o.constructor) n = o.constructor.name;
137
+ if (n === "Map" || n === "Set") return Array.from(o);
138
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
139
+ }
140
+
141
+ function _arrayLikeToArray(arr, len) {
142
+ if (len == null || len > arr.length) len = arr.length;
143
+
144
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
145
+
146
+ return arr2;
147
+ }
148
+
149
+ function _createForOfIteratorHelper(o, allowArrayLike) {
150
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
151
+
152
+ if (!it) {
153
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
154
+ if (it) o = it;
155
+ var i = 0;
156
+
157
+ var F = function () {};
158
+
159
+ return {
160
+ s: F,
161
+ n: function () {
162
+ if (i >= o.length) return {
163
+ done: true
164
+ };
165
+ return {
166
+ done: false,
167
+ value: o[i++]
168
+ };
169
+ },
170
+ e: function (e) {
171
+ throw e;
172
+ },
173
+ f: F
174
+ };
175
+ }
176
+
177
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
178
+ }
179
+
180
+ var normalCompletion = true,
181
+ didErr = false,
182
+ err;
183
+ return {
184
+ s: function () {
185
+ it = it.call(o);
186
+ },
187
+ n: function () {
188
+ var step = it.next();
189
+ normalCompletion = step.done;
190
+ return step;
191
+ },
192
+ e: function (e) {
193
+ didErr = true;
194
+ err = e;
195
+ },
196
+ f: function () {
197
+ try {
198
+ if (!normalCompletion && it.return != null) it.return();
199
+ } finally {
200
+ if (didErr) throw err;
201
+ }
202
+ }
203
+ };
204
+ }
205
+
132
206
  class Connector {
133
207
  /**
134
208
  * Create a new class instance.
@@ -190,6 +264,7 @@ var Websocket = /*#__PURE__*/function () {
190
264
  this.internalListeners = {};
191
265
  this.channelBacklog = [];
192
266
  this.closing = false;
267
+ this.hasConnected = false;
193
268
  this.options = options;
194
269
  this.connect(this.options.host);
195
270
  return this;
@@ -200,10 +275,23 @@ var Websocket = /*#__PURE__*/function () {
200
275
  value: function connect(host) {
201
276
  var _this = this;
202
277
 
203
- this.options.debug && console.log('Connecting');
278
+ this.options.debug && console.log('Trying to connect...');
204
279
  this.websocket = new WebSocket(host);
205
280
 
281
+ this.websocket.onerror = function () {
282
+ if (!_this.hasConnected) {
283
+ setTimeout(function () {
284
+ _this.socketId = undefined;
285
+
286
+ _this.connect(host);
287
+ }, 3000);
288
+ }
289
+ };
290
+
206
291
  this.websocket.onopen = function () {
292
+ _this.options.debug && console.log('Connected !');
293
+ _this.hasConnected = true;
294
+
207
295
  _this.send({
208
296
  event: 'whoami'
209
297
  });
@@ -214,65 +302,83 @@ var Websocket = /*#__PURE__*/function () {
214
302
  _this.send(message);
215
303
 
216
304
  _this.buffer.splice(0, 1);
217
- }
218
- };
305
+ } // Register events only once connected, or they won't be registered if connection failed/lost
219
306
 
220
- this.websocket.onmessage = function (messageEvent) {
221
- var message = _this.parseMessage(messageEvent.data);
222
307
 
223
- if (!message) {
224
- return;
225
- }
308
+ _this.websocket.onmessage = function (messageEvent) {
309
+ var message = _this.parseMessage(messageEvent.data);
226
310
 
227
- if (message.channel) {
228
- _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
311
+ _this.options.debug && console.log('onmessage', messageEvent.data);
229
312
 
230
- if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
231
- _this.listeners[message.channel][message.event](message.data);
313
+ if (!message) {
314
+ return;
232
315
  }
233
316
 
234
- return;
235
- }
317
+ if (message.channel) {
318
+ _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
236
319
 
237
- if (_this.internalListeners[message.event]) {
238
- _this.internalListeners[message.event](message.data);
239
- }
320
+ if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
321
+ _this.listeners[message.channel][message.event](message.data);
322
+ }
323
+
324
+ return;
325
+ }
326
+
327
+ if (_this.internalListeners[message.event]) {
328
+ _this.internalListeners[message.event](message.data);
329
+ }
330
+ }; // send ping every 60 seconds to keep connection alive
331
+
332
+
333
+ _this.pingInterval = setInterval(function () {
334
+ if (_this.websocket.readyState === _this.websocket.OPEN) {
335
+ _this.options.debug && console.log('Sending ping');
336
+
337
+ _this.send({
338
+ event: 'ping'
339
+ });
340
+ }
341
+ }, 60 * 1000);
240
342
  };
241
343
 
242
344
  this.websocket.onclose = function () {
243
- if (_this.socketId && !_this.closing || !_this.socketId) {
244
- _this.options.debug && console.info('Connection lost, reconnecting...');
245
- setTimeout(function () {
246
- _this.socketId = undefined;
345
+ _this.options.debug && console.info('Connection closed.');
247
346
 
248
- _this.connect(host);
249
- }, 1000);
347
+ if (_this.closing) {
348
+ return;
250
349
  }
350
+
351
+ _this.hasConnected = false;
352
+ _this.options.debug && console.info('Connection lost, reconnecting...');
353
+ setTimeout(function () {
354
+ _this.socketId = undefined;
355
+
356
+ _this.connect(host);
357
+ }, 1000);
251
358
  };
252
359
 
253
360
  this.on('whoami', function (_ref) {
254
361
  var socketId = _ref.socket_id;
255
362
  _this.socketId = socketId;
256
- _this.options.debug && console.log("just set socketId to ".concat(socketId));
257
-
258
- while (_this.channelBacklog.length) {
259
- var channel = _this.channelBacklog[0];
363
+ _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
260
364
 
261
- _this.actuallySubscribe(channel);
365
+ var channel;
262
366
 
263
- _this.channelBacklog.splice(0, 1);
264
- }
265
- }); // send ping every 60 seconds to keep connection alive
367
+ var _iterator = _createForOfIteratorHelper(_this.channelBacklog),
368
+ _step;
266
369
 
267
- this.pingInterval = setInterval(function () {
268
- if (_this.websocket.readyState === _this.websocket.OPEN) {
269
- _this.options.debug && console.log('Sending ping');
370
+ try {
371
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
372
+ channel = _step.value;
270
373
 
271
- _this.send({
272
- event: 'ping'
273
- });
374
+ _this.actuallySubscribe(channel);
375
+ }
376
+ } catch (err) {
377
+ _iterator.e(err);
378
+ } finally {
379
+ _iterator.f();
274
380
  }
275
- }, 60 * 1000);
381
+ });
276
382
  }
277
383
  }, {
278
384
  key: "parseMessage",
@@ -128,6 +128,80 @@ var LaravelEchoApiGateway = (function (exports) {
128
128
  };
129
129
  }
130
130
 
131
+ function _unsupportedIterableToArray(o, minLen) {
132
+ if (!o) return;
133
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
134
+ var n = Object.prototype.toString.call(o).slice(8, -1);
135
+ if (n === "Object" && o.constructor) n = o.constructor.name;
136
+ if (n === "Map" || n === "Set") return Array.from(o);
137
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
138
+ }
139
+
140
+ function _arrayLikeToArray(arr, len) {
141
+ if (len == null || len > arr.length) len = arr.length;
142
+
143
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
144
+
145
+ return arr2;
146
+ }
147
+
148
+ function _createForOfIteratorHelper(o, allowArrayLike) {
149
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
150
+
151
+ if (!it) {
152
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
153
+ if (it) o = it;
154
+ var i = 0;
155
+
156
+ var F = function () {};
157
+
158
+ return {
159
+ s: F,
160
+ n: function () {
161
+ if (i >= o.length) return {
162
+ done: true
163
+ };
164
+ return {
165
+ done: false,
166
+ value: o[i++]
167
+ };
168
+ },
169
+ e: function (e) {
170
+ throw e;
171
+ },
172
+ f: F
173
+ };
174
+ }
175
+
176
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
177
+ }
178
+
179
+ var normalCompletion = true,
180
+ didErr = false,
181
+ err;
182
+ return {
183
+ s: function () {
184
+ it = it.call(o);
185
+ },
186
+ n: function () {
187
+ var step = it.next();
188
+ normalCompletion = step.done;
189
+ return step;
190
+ },
191
+ e: function (e) {
192
+ didErr = true;
193
+ err = e;
194
+ },
195
+ f: function () {
196
+ try {
197
+ if (!normalCompletion && it.return != null) it.return();
198
+ } finally {
199
+ if (didErr) throw err;
200
+ }
201
+ }
202
+ };
203
+ }
204
+
131
205
  class Connector {
132
206
  /**
133
207
  * Create a new class instance.
@@ -189,6 +263,7 @@ var LaravelEchoApiGateway = (function (exports) {
189
263
  this.internalListeners = {};
190
264
  this.channelBacklog = [];
191
265
  this.closing = false;
266
+ this.hasConnected = false;
192
267
  this.options = options;
193
268
  this.connect(this.options.host);
194
269
  return this;
@@ -199,10 +274,23 @@ var LaravelEchoApiGateway = (function (exports) {
199
274
  value: function connect(host) {
200
275
  var _this = this;
201
276
 
202
- this.options.debug && console.log('Connecting');
277
+ this.options.debug && console.log('Trying to connect...');
203
278
  this.websocket = new WebSocket(host);
204
279
 
280
+ this.websocket.onerror = function () {
281
+ if (!_this.hasConnected) {
282
+ setTimeout(function () {
283
+ _this.socketId = undefined;
284
+
285
+ _this.connect(host);
286
+ }, 3000);
287
+ }
288
+ };
289
+
205
290
  this.websocket.onopen = function () {
291
+ _this.options.debug && console.log('Connected !');
292
+ _this.hasConnected = true;
293
+
206
294
  _this.send({
207
295
  event: 'whoami'
208
296
  });
@@ -213,65 +301,83 @@ var LaravelEchoApiGateway = (function (exports) {
213
301
  _this.send(message);
214
302
 
215
303
  _this.buffer.splice(0, 1);
216
- }
217
- };
304
+ } // Register events only once connected, or they won't be registered if connection failed/lost
218
305
 
219
- this.websocket.onmessage = function (messageEvent) {
220
- var message = _this.parseMessage(messageEvent.data);
221
306
 
222
- if (!message) {
223
- return;
224
- }
307
+ _this.websocket.onmessage = function (messageEvent) {
308
+ var message = _this.parseMessage(messageEvent.data);
225
309
 
226
- if (message.channel) {
227
- _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
310
+ _this.options.debug && console.log('onmessage', messageEvent.data);
228
311
 
229
- if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
230
- _this.listeners[message.channel][message.event](message.data);
312
+ if (!message) {
313
+ return;
231
314
  }
232
315
 
233
- return;
234
- }
316
+ if (message.channel) {
317
+ _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
235
318
 
236
- if (_this.internalListeners[message.event]) {
237
- _this.internalListeners[message.event](message.data);
238
- }
319
+ if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
320
+ _this.listeners[message.channel][message.event](message.data);
321
+ }
322
+
323
+ return;
324
+ }
325
+
326
+ if (_this.internalListeners[message.event]) {
327
+ _this.internalListeners[message.event](message.data);
328
+ }
329
+ }; // send ping every 60 seconds to keep connection alive
330
+
331
+
332
+ _this.pingInterval = setInterval(function () {
333
+ if (_this.websocket.readyState === _this.websocket.OPEN) {
334
+ _this.options.debug && console.log('Sending ping');
335
+
336
+ _this.send({
337
+ event: 'ping'
338
+ });
339
+ }
340
+ }, 60 * 1000);
239
341
  };
240
342
 
241
343
  this.websocket.onclose = function () {
242
- if (_this.socketId && !_this.closing || !_this.socketId) {
243
- _this.options.debug && console.info('Connection lost, reconnecting...');
244
- setTimeout(function () {
245
- _this.socketId = undefined;
344
+ _this.options.debug && console.info('Connection closed.');
246
345
 
247
- _this.connect(host);
248
- }, 1000);
346
+ if (_this.closing) {
347
+ return;
249
348
  }
349
+
350
+ _this.hasConnected = false;
351
+ _this.options.debug && console.info('Connection lost, reconnecting...');
352
+ setTimeout(function () {
353
+ _this.socketId = undefined;
354
+
355
+ _this.connect(host);
356
+ }, 1000);
250
357
  };
251
358
 
252
359
  this.on('whoami', function (_ref) {
253
360
  var socketId = _ref.socket_id;
254
361
  _this.socketId = socketId;
255
- _this.options.debug && console.log("just set socketId to ".concat(socketId));
256
-
257
- while (_this.channelBacklog.length) {
258
- var channel = _this.channelBacklog[0];
362
+ _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
259
363
 
260
- _this.actuallySubscribe(channel);
364
+ var channel;
261
365
 
262
- _this.channelBacklog.splice(0, 1);
263
- }
264
- }); // send ping every 60 seconds to keep connection alive
366
+ var _iterator = _createForOfIteratorHelper(_this.channelBacklog),
367
+ _step;
265
368
 
266
- this.pingInterval = setInterval(function () {
267
- if (_this.websocket.readyState === _this.websocket.OPEN) {
268
- _this.options.debug && console.log('Sending ping');
369
+ try {
370
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
371
+ channel = _step.value;
269
372
 
270
- _this.send({
271
- event: 'ping'
272
- });
373
+ _this.actuallySubscribe(channel);
374
+ }
375
+ } catch (err) {
376
+ _iterator.e(err);
377
+ } finally {
378
+ _iterator.f();
273
379
  }
274
- }, 60 * 1000);
380
+ });
275
381
  }
276
382
  }, {
277
383
  key: "parseMessage",
@@ -125,6 +125,80 @@ function _createSuper(Derived) {
125
125
  };
126
126
  }
127
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
+
128
202
  class Connector {
129
203
  /**
130
204
  * Create a new class instance.
@@ -186,6 +260,7 @@ var Websocket = /*#__PURE__*/function () {
186
260
  this.internalListeners = {};
187
261
  this.channelBacklog = [];
188
262
  this.closing = false;
263
+ this.hasConnected = false;
189
264
  this.options = options;
190
265
  this.connect(this.options.host);
191
266
  return this;
@@ -196,10 +271,23 @@ var Websocket = /*#__PURE__*/function () {
196
271
  value: function connect(host) {
197
272
  var _this = this;
198
273
 
199
- this.options.debug && console.log('Connecting');
274
+ this.options.debug && console.log('Trying to connect...');
200
275
  this.websocket = new WebSocket(host);
201
276
 
277
+ this.websocket.onerror = function () {
278
+ if (!_this.hasConnected) {
279
+ setTimeout(function () {
280
+ _this.socketId = undefined;
281
+
282
+ _this.connect(host);
283
+ }, 3000);
284
+ }
285
+ };
286
+
202
287
  this.websocket.onopen = function () {
288
+ _this.options.debug && console.log('Connected !');
289
+ _this.hasConnected = true;
290
+
203
291
  _this.send({
204
292
  event: 'whoami'
205
293
  });
@@ -210,65 +298,83 @@ var Websocket = /*#__PURE__*/function () {
210
298
  _this.send(message);
211
299
 
212
300
  _this.buffer.splice(0, 1);
213
- }
214
- };
301
+ } // Register events only once connected, or they won't be registered if connection failed/lost
215
302
 
216
- this.websocket.onmessage = function (messageEvent) {
217
- var message = _this.parseMessage(messageEvent.data);
218
303
 
219
- if (!message) {
220
- return;
221
- }
304
+ _this.websocket.onmessage = function (messageEvent) {
305
+ var message = _this.parseMessage(messageEvent.data);
222
306
 
223
- if (message.channel) {
224
- _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
307
+ _this.options.debug && console.log('onmessage', messageEvent.data);
225
308
 
226
- if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
227
- _this.listeners[message.channel][message.event](message.data);
309
+ if (!message) {
310
+ return;
228
311
  }
229
312
 
230
- return;
231
- }
313
+ if (message.channel) {
314
+ _this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
232
315
 
233
- if (_this.internalListeners[message.event]) {
234
- _this.internalListeners[message.event](message.data);
235
- }
316
+ if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
317
+ _this.listeners[message.channel][message.event](message.data);
318
+ }
319
+
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);
236
338
  };
237
339
 
238
340
  this.websocket.onclose = function () {
239
- if (_this.socketId && !_this.closing || !_this.socketId) {
240
- _this.options.debug && console.info('Connection lost, reconnecting...');
241
- setTimeout(function () {
242
- _this.socketId = undefined;
341
+ _this.options.debug && console.info('Connection closed.');
243
342
 
244
- _this.connect(host);
245
- }, 1000);
343
+ if (_this.closing) {
344
+ return;
246
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);
247
354
  };
248
355
 
249
356
  this.on('whoami', function (_ref) {
250
357
  var socketId = _ref.socket_id;
251
358
  _this.socketId = socketId;
252
- _this.options.debug && console.log("just set socketId to ".concat(socketId));
253
-
254
- while (_this.channelBacklog.length) {
255
- var channel = _this.channelBacklog[0];
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
256
360
 
257
- _this.actuallySubscribe(channel);
361
+ var channel;
258
362
 
259
- _this.channelBacklog.splice(0, 1);
260
- }
261
- }); // send ping every 60 seconds to keep connection alive
363
+ var _iterator = _createForOfIteratorHelper(_this.channelBacklog),
364
+ _step;
262
365
 
263
- this.pingInterval = setInterval(function () {
264
- if (_this.websocket.readyState === _this.websocket.OPEN) {
265
- _this.options.debug && console.log('Sending ping');
366
+ try {
367
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
368
+ channel = _step.value;
266
369
 
267
- _this.send({
268
- event: 'ping'
269
- });
370
+ _this.actuallySubscribe(channel);
371
+ }
372
+ } catch (err) {
373
+ _iterator.e(err);
374
+ } finally {
375
+ _iterator.f();
270
376
  }
271
- }, 60 * 1000);
377
+ });
272
378
  }
273
379
  }, {
274
380
  key: "parseMessage",
@@ -21,15 +21,27 @@ export class Websocket {
21
21
  private socketId: string;
22
22
 
23
23
  private closing = false;
24
+ private hasConnected = false;
24
25
 
25
26
  private pingInterval: NodeJS.Timeout;
26
27
 
27
28
  private connect(host: string): void {
28
- this.options.debug && console.log('Connecting');
29
+ this.options.debug && console.log('Trying to connect...');
29
30
 
30
31
  this.websocket = new WebSocket(host)
31
32
 
33
+ this.websocket.onerror = () => {
34
+ if (!this.hasConnected) {
35
+ setTimeout(() => {
36
+ this.socketId = undefined
37
+ this.connect(host)
38
+ }, 3000);
39
+ }
40
+ }
41
+
32
42
  this.websocket.onopen = () => {
43
+ this.options.debug && console.log('Connected !');
44
+ this.hasConnected = true;
33
45
  this.send({
34
46
  event: 'whoami',
35
47
  })
@@ -41,40 +53,56 @@ export class Websocket {
41
53
 
42
54
  this.buffer.splice(0, 1)
43
55
  }
44
- }
45
56
 
46
- this.websocket.onmessage = (messageEvent: MessageEvent) => {
47
- const message = this.parseMessage(messageEvent.data)
57
+ // Register events only once connected, or they won't be registered if connection failed/lost
48
58
 
49
- if (!message) {
50
- return
51
- }
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
+ }
66
+
67
+ if (message.channel) {
68
+ this.options.debug && console.log(`Received event ${message.event} on channel ${message.channel}`)
52
69
 
53
- if (message.channel) {
54
- this.options.debug && console.log(`Received event ${message.event} on channel ${message.channel}`)
70
+ if (this.listeners[message.channel] && this.listeners[message.channel][message.event]) {
71
+ this.listeners[message.channel][message.event](message.data)
72
+ }
55
73
 
56
- if (this.listeners[message.channel] && this.listeners[message.channel][message.event]) {
57
- this.listeners[message.channel][message.event](message.data)
74
+ return
58
75
  }
59
76
 
60
- return
77
+ if (this.internalListeners[message.event]) {
78
+ this.internalListeners[message.event](message.data)
79
+ }
61
80
  }
62
81
 
63
- if (this.internalListeners[message.event]) {
64
- this.internalListeners[message.event](message.data)
65
- }
66
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)
67
92
  }
68
93
 
69
94
 
70
95
  this.websocket.onclose = () => {
71
- if (this.socketId && !this.closing || !this.socketId) {
72
- this.options.debug && console.info('Connection lost, reconnecting...');
73
- setTimeout(() => {
74
- this.socketId = undefined
75
- this.connect(host)
76
- }, 1000);
96
+ this.options.debug && console.info('Connection closed.');
97
+ if (this.closing){
98
+ return;
77
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);
78
106
  };
79
107
 
80
108
  this.on('whoami', ({ socket_id: socketId }) => {
@@ -82,25 +110,14 @@ export class Websocket {
82
110
 
83
111
  this.options.debug && console.log(`just set socketId to ${socketId}`)
84
112
 
85
- while (this.channelBacklog.length) {
86
- const channel = this.channelBacklog[0]
87
-
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){
88
116
  this.actuallySubscribe(channel)
89
-
90
- this.channelBacklog.splice(0, 1)
91
117
  }
92
- })
93
118
 
119
+ })
94
120
 
95
- // send ping every 60 seconds to keep connection alive
96
- this.pingInterval = setInterval(() => {
97
- if (this.websocket.readyState === this.websocket.OPEN) {
98
- this.options.debug && console.log('Sending ping')
99
- this.send({
100
- event: 'ping',
101
- })
102
- }
103
- }, 60 * 1000)
104
121
 
105
122
  }
106
123
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "laravel-echo-api-gateway",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Use Laravel Echo with API Gateway Websockets",
5
5
  "keywords": [
6
6
  "laravel",