Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/infrastructure/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/infrastructure/Listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
});
});
});