Skip to content

Commit

Permalink
fix(WebChannelGate): close event code when key already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Jul 29, 2016
1 parent 7f916f8 commit 4f2606b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dist/netflux.es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,7 @@ class WebChannelGate {
webSocketService.connect(url)
.then((ws) => {
ws.onclose = (closeEvt) => {
if (closeEvt.code !== 1000) { reject(closeEvt.reason) }
reject(closeEvt.reason)
this.onClose(closeEvt)
}
ws.onerror = (err) => reject(err.message)
Expand All @@ -2719,7 +2719,7 @@ class WebChannelGate {
try {
ws.send(JSON.stringify({key}))
// FIXME: find a better solution than setTimeout. This is for the case when the key already exists and thus the server will close the socket, but it will close it after this function resolves the Promise.
setTimeout(() => { resolve(this.accessData) }, 300, {url, key})
setTimeout(() => { resolve(this.accessData) }, 700, {url, key})
} catch (err) {
reject(err.message)
}
Expand Down
4 changes: 2 additions & 2 deletions dist/netflux.es2015.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@
webSocketService.connect(url)
.then((ws) => {
ws.onclose = (closeEvt) => {
if (closeEvt.code !== 1000) { reject(closeEvt.reason) }
reject(closeEvt.reason)
this.onClose(closeEvt)
}
ws.onerror = (err) => reject(err.message)
Expand All @@ -2725,7 +2725,7 @@
try {
ws.send(JSON.stringify({key}))
// FIXME: find a better solution than setTimeout. This is for the case when the key already exists and thus the server will close the socket, but it will close it after this function resolves the Promise.
setTimeout(() => { resolve(this.accessData) }, 300, {url, key})
setTimeout(() => { resolve(this.accessData) }, 700, {url, key})
} catch (err) {
reject(err.message)
}
Expand Down
4 changes: 2 additions & 2 deletions src/WebChannelGate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class WebChannelGate {
webSocketService.connect(url)
.then((ws) => {
ws.onclose = (closeEvt) => {
if (closeEvt.code !== 1000) { reject(closeEvt.reason) }
reject(closeEvt.reason)
this.onClose(closeEvt)
}
ws.onerror = (err) => reject(err.message)
Expand All @@ -73,7 +73,7 @@ class WebChannelGate {
try {
ws.send(JSON.stringify({key}))
// FIXME: find a better solution than setTimeout. This is for the case when the key already exists and thus the server will close the socket, but it will close it after this function resolves the Promise.
setTimeout(() => { resolve(this.accessData) }, 300, {url, key})
setTimeout(() => { resolve(this.accessData) }, 700, {url, key})
} catch (err) {
reject(err.message)
}
Expand Down
36 changes: 34 additions & 2 deletions test/WebChannelGate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,41 @@ describe('WebChannelGate', () => {
let wcg2 = new WebChannelGate()
wcg1.open(() => {}, {signaling, key}).then(() => {
wcg2.open(() => {}, {signaling, key}).then(() => {
console.log('CONNECTED')
done.fail()
}).catch(done)
}).catch(() => {
wcg1.close()
done()
})
}).catch(done.fail)
})

it('Not Secure', (done) => {
let key = webChannelGate.generateKey()
let wcg1 = new WebChannelGate()
let wcg2 = new WebChannelGate()
let signaling = 'ws://sigver-coastteam.rhcloud.com:8000'
wcg1.open(() => {}, {signaling, key}).then(() => {
wcg2.open(() => {}, {signaling, key}).then(() => {
done.fail()
}).catch(() => {
wcg1.close()
done()
})
}).catch(done.fail)
})

it('Secure', (done) => {
let key = webChannelGate.generateKey()
let wcg1 = new WebChannelGate()
let wcg2 = new WebChannelGate()
let signaling = 'wss://sigver-coastteam.rhcloud.com:8443'
wcg1.open(() => {}, {signaling, key}).then(() => {
wcg2.open(() => {}, {signaling, key}).then(() => {
done.fail()
}).catch(() => {
wcg1.close()
done()
})
}).catch(done.fail)
})
})
2 changes: 1 addition & 1 deletion test/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const signaling = 'ws://localhost:8000'
// const signaling = 'wss://sigver-coastteam.rhcloud.com:443'
// const signaling = 'wss://sigver-coastteam.rhcloud.com:8443'
// const signaling = 'ws://sigver-coastteam.rhcloud.com:8000'
export const MSG_NUMBER = 10

Expand Down

0 comments on commit 4f2606b

Please sign in to comment.