From 4f2606b644193d5aa0b1994681c97c3fc42e6034 Mon Sep 17 00:00:00 2001 From: Philippe Kalitine Date: Fri, 29 Jul 2016 16:39:33 +0200 Subject: [PATCH] fix(WebChannelGate): close event code when key already exist --- dist/netflux.es2015.js | 4 ++-- dist/netflux.es2015.umd.js | 4 ++-- src/WebChannelGate.js | 4 ++-- test/WebChannelGate.test.js | 36 ++++++++++++++++++++++++++++++++++-- test/config.js | 2 +- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/dist/netflux.es2015.js b/dist/netflux.es2015.js index ee049363..49feb2a9 100644 --- a/dist/netflux.es2015.js +++ b/dist/netflux.es2015.js @@ -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) @@ -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) } diff --git a/dist/netflux.es2015.umd.js b/dist/netflux.es2015.umd.js index 2aedf3fa..69da908f 100644 --- a/dist/netflux.es2015.umd.js +++ b/dist/netflux.es2015.umd.js @@ -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) @@ -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) } diff --git a/src/WebChannelGate.js b/src/WebChannelGate.js index b919a6ea..f21eeba2 100644 --- a/src/WebChannelGate.js +++ b/src/WebChannelGate.js @@ -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) @@ -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) } diff --git a/test/WebChannelGate.test.js b/test/WebChannelGate.test.js index 3a662b5a..668e0248 100644 --- a/test/WebChannelGate.test.js +++ b/test/WebChannelGate.test.js @@ -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) }) }) diff --git a/test/config.js b/test/config.js index f2fff897..f0aeed25 100644 --- a/test/config.js +++ b/test/config.js @@ -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