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

Commit 8be53d4

Browse files
authored
Merge pull request #51 from cubic-js/development
fix: Re-implement retry if initial connects failed
2 parents 96e44a4 + 26605b3 commit 8be53d4

File tree

5 files changed

+51
-39
lines changed

5 files changed

+51
-39
lines changed

lib/index.js

Lines changed: 29 additions & 23 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.

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"babelrc-rollup": "^3.0.0",
2727
"cubic-api": "1.2.1",
2828
"cubic-auth": "1.1.0",
29-
"cubic-client": "^1.0.11",
29+
"cubic-client": "^1.1.0",
3030
"cubic-core": "1.1.1",
3131
"cubic-loader": "1.1.1",
3232
"eslint": "^5.1.0",

src/connection.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Connection {
2525
/**
2626
* Socket.io client with currently stored tokens
2727
*/
28-
async setClient () {
28+
async setClient (skipListeners) {
2929
let sioConfig = this.auth.access_token ? {
3030
query: 'bearer=' + this.auth.access_token,
3131
reconnection: false
@@ -37,16 +37,22 @@ class Connection {
3737
this.client = io.connect(this.options.api_url + this.options.namespace, sioConfig)
3838

3939
// Event listeners
40-
this.client.on('error', console.error)
41-
this.client.on('connect_error', () => this.reload())
42-
this.client.on('disconnect', () => this.reload())
43-
this.client.on('connect', () => {
44-
this.reconnecting = false
45-
this.subscriptions.forEach(sub => this.client.emit('subscribe', sub))
46-
})
47-
this.client.on('subscribed', sub => {
48-
if (!this.subscriptions.includes(sub)) this.subscriptions.push(sub)
49-
})
40+
if (!skipListeners) {
41+
this.client.on('error', console.error)
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+
}, 1000)
5056
}
5157

5258
/**

0 commit comments

Comments
 (0)