Skip to content

Commit

Permalink
Merge pull request #265 from GetStream/vishal/disconnect-ws-fix
Browse files Browse the repository at this point in the history
Increment wsId when ws is disconnected manually
  • Loading branch information
vishalnarkhede committed Mar 3, 2020
2 parents 6603641 + 15a742a commit 792de5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export class StableWSConnection {
},
);

this.wsID += 1;

// start by removing all the listeners
if (this.healthCheckIntervalRef) {
clearInterval(this.healthCheckIntervalRef);
Expand All @@ -121,8 +123,6 @@ export class StableWSConnection {

this._removeConnectionListeners();

// reset the wsID;
this.wsID = 1;
this.isHealthy = false;

// remove ws handlers...
Expand Down Expand Up @@ -450,10 +450,14 @@ export class StableWSConnection {
*
*/
_errorFromWSEvent = event => {
this.logger('error', `connection:onclose() - WS failed with code ${event.code}`, {
tags: ['connection'],
event,
});
this.logger(
'error',
`connection:_errorFromWSEvent() - WS failed with code ${event.code}`,
{
tags: ['connection'],
event,
},
);

const error = new Error(`WS failed with code ${event.code}`);
error.code = event.code;
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,21 @@ describe('Chat', function() {
expect(updatedUser.book).to.equal('dune');
});

it.only('manual disconnect and reconnect flow', async () => {
const userID = uuidv4();
const client = getTestClient(false);
const token = createUserToken(userID);
await client.setUser({ id: userID }, token);

await client.wsConnection.disconnect();
expect(client.wsConnection.isHealthy).to.equal(false);
expect(client.wsConnection.wsID).to.equal(2);
expect(client.wsConnection.ws).to.equal(undefined);

await client._setupConnection();
expect(client.wsConnection.isHealthy).to.equal(true);
expect(client.wsConnection.ws).to.not.equal(undefined);
});
context('When user is banned', function() {
const banned = uuidv4();
const token = createUserToken(banned);
Expand Down

0 comments on commit 792de5b

Please sign in to comment.