Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 4fe6682

Browse files
authored
Merge pull request #69 from cubic-js/development
Reimplement forced reconnect on failing initial connection.
2 parents e1f3733 + 4157c4d commit 4fe6682

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

lib/index.js

Lines changed: 38 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connection.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Auth from './auth.js'
22
import ServerError from './serverError.js'
33
const io = require('socket.io-client')
44
const queue = require('async-delay-queue')
5+
const timeout = (fn, s) => {
6+
return new Promise(resolve => setTimeout(() => resolve(fn()), s))
7+
}
58

69
class Connection {
710
constructor (options) {
@@ -22,7 +25,7 @@ class Connection {
2225
/**
2326
* Socket.io client with currently stored tokens
2427
*/
25-
async setClient () {
28+
async setClient (skipListeners) {
2629
let sioConfig = this.auth.access_token ? {
2730
query: 'bearer=' + this.auth.access_token,
2831
reconnection: true
@@ -32,16 +35,24 @@ class Connection {
3235

3336
// Connect to parent namespace
3437
this.client = io.connect(this.options.api_url + this.options.namespace, sioConfig)
35-
this.client.on('error', () => this.reload())
36-
this.client.on('connect_error', () => this.reload())
37-
this.client.on('disconnect', () => this.reload())
38-
this.client.on('connect', () => {
39-
this.reconnecting = false
40-
this.subscriptions.forEach(sub => this.client.emit('subscribe', sub))
41-
})
42-
this.client.on('subscribed', sub => {
43-
if (!this.subscriptions.includes(sub)) this.subscriptions.push(sub)
44-
})
38+
39+
// Event listeners
40+
if (!skipListeners) {
41+
this.client.on('error', () => this.reload())
42+
this.client.on('connect_error', () => this.reload())
43+
this.client.on('disconnect', () => this.reload())
44+
this.client.on('connect', () => {
45+
this.reconnecting = false
46+
this.subscriptions.forEach(sub => this.client.emit('subscribe', sub))
47+
})
48+
this.client.on('subscribed', sub => {
49+
if (!this.subscriptions.includes(sub)) this.subscriptions.push(sub)
50+
})
51+
}
52+
53+
await timeout(() => {
54+
if (!this.client.connected) this.setClient(true)
55+
}, 10000)
4556
}
4657

4758
/**

0 commit comments

Comments
 (0)