Skip to content

Commit 684ed22

Browse files
author
Grégory Saive
authored
Merge pull request #23 from jorisadri/iss22
#22 Fix handle error websocket
2 parents 894f8c5 + 9a8b921 commit 684ed22

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/infrastructure/Listener.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export class Listener {
104104
this.webSocket.onerror = (err) => {
105105
console.log('WebSocket Error ');
106106
console.log(err);
107+
reject(err);
107108
};
108109
this.webSocket.onmessage = (msg) => {
109110
const message = JSON.parse(msg.data as string);
@@ -155,6 +156,17 @@ export class Listener {
155156
});
156157
}
157158

159+
/**
160+
* returns a boolean that repressents the open state
161+
* @returns a boolean
162+
*/
163+
public isOpen(): boolean {
164+
if(this.webSocket){
165+
return this.webSocket.readyState === WebSocket.OPEN;
166+
}
167+
return false;
168+
}
169+
158170
/**
159171
* Close web socket connection.
160172
* @returns void

test/infrastructure/Listener.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,25 @@ describe('Listener', () => {
2323
expect('ws://localhost:3000/ws').to.be.equal(listener.url);
2424
listener.close();
2525
});
26+
27+
describe('isOpen', () => {
28+
it('should return false when listener is created and not opened', () => {
29+
const listener = new Listener('ws://localhost:3000');
30+
expect(listener.isOpen()).to.be.false;
31+
listener.close();
32+
});
33+
});
34+
35+
describe('onerror', () => {
36+
it('should reject because of wrong server url', async () => {
37+
const listener = new Listener('https://notcorrecturl:0000');
38+
await listener.open()
39+
.then((result) => {
40+
throw new Error('This should not be called when expecting error');
41+
})
42+
.catch((error) => {
43+
expect(error.toString()).to.be.equal("Error: getaddrinfo ENOTFOUND notcorrecturl notcorrecturl:0000");
44+
})
45+
});
46+
});
2647
});

0 commit comments

Comments
 (0)