diff --git a/src/infrastructure/Listener.ts b/src/infrastructure/Listener.ts index 00fbcb248c..ad4a2cfa63 100644 --- a/src/infrastructure/Listener.ts +++ b/src/infrastructure/Listener.ts @@ -104,6 +104,7 @@ export class Listener { this.webSocket.onerror = (err) => { console.log('WebSocket Error '); console.log(err); + reject(err); }; this.webSocket.onmessage = (msg) => { const message = JSON.parse(msg.data as string); @@ -155,6 +156,17 @@ export class Listener { }); } + /** + * returns a boolean that repressents the open state + * @returns a boolean + */ + public isOpen(): boolean { + if(this.webSocket){ + return this.webSocket.readyState === WebSocket.OPEN; + } + return false; + } + /** * Close web socket connection. * @returns void diff --git a/test/infrastructure/Listener.spec.ts b/test/infrastructure/Listener.spec.ts index 87af430852..b94c72afd7 100644 --- a/test/infrastructure/Listener.spec.ts +++ b/test/infrastructure/Listener.spec.ts @@ -23,4 +23,25 @@ describe('Listener', () => { expect('ws://localhost:3000/ws').to.be.equal(listener.url); listener.close(); }); + + describe('isOpen', () => { + it('should return false when listener is created and not opened', () => { + const listener = new Listener('ws://localhost:3000'); + expect(listener.isOpen()).to.be.false; + listener.close(); + }); + }); + + describe('onerror', () => { + it('should reject because of wrong server url', async () => { + const listener = new Listener('https://notcorrecturl:0000'); + await listener.open() + .then((result) => { + throw new Error('This should not be called when expecting error'); + }) + .catch((error) => { + expect(error.toString()).to.be.equal("Error: getaddrinfo ENOTFOUND notcorrecturl notcorrecturl:0000"); + }) + }); + }); });