Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #164 from apollographql/bugfix/reconnect
Browse files Browse the repository at this point in the history
Fix for reconnect after manual close
  • Loading branch information
Urigo authored Jun 4, 2017
2 parents dff098e + 6170307 commit 2fd7287
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,18 @@ export class SubscriptionClient {

public get status() {
if (this.client === null) {
return 0; //readyState 'CONNECTING'
return this.wsImpl.CLOSED;
}

return this.client.readyState;
}

public close() {
public close(isForced = true) {
if (this.client !== null) {
this.forceClose = true;
this.forceClose = isForced;
this.sendMessage(undefined, MessageTypes.GQL_CONNECTION_TERMINATE, null);
this.client.close();
this.client = null;
}
}

Expand Down Expand Up @@ -423,7 +424,7 @@ export class SubscriptionClient {
}

private checkConnection() {
this.wasKeepAliveReceived ? this.wasKeepAliveReceived = false : this.close();
this.wasKeepAliveReceived ? this.wasKeepAliveReceived = false : this.close(false);
}

private connect() {
Expand Down
41 changes: 41 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,47 @@ describe('Message Types', function () {
});

describe('Client<->Server Flow', () => {
it('should reconnect after manually closing the connection and then resubscribing', (done) => {
const testServer = createServer(notFoundRequestListener);
testServer.listen(SERVER_EXECUTOR_TESTS_PORT);

SubscriptionServer.create({
schema: subscriptionsSchema,
execute,
subscribe,
}, {
server: testServer,
path: '/',
});

const client = new SubscriptionClient(`ws://localhost:${SERVER_EXECUTOR_TESTS_PORT}/`);
let isFirstTime = true;

client.onConnected(async () => {
// Manually close the connection only in the first time, to avoid infinite loop
if (isFirstTime) {
isFirstTime = false;

setTimeout(() => {
// Disconnect the client
client.close();

// Subscribe to data, without manually reconnect before
const opId = client.subscribe({
query: `query { testString }`,
variables: {},
}, (err, res) => {
expect(opId).not.to.eq(null);
expect(err).to.eq(null);
expect(res.testString).to.eq('value');
testServer.close();
done();
});
}, 300);
}
});
});

it('should close iteration over AsyncIterator when client unsubscribes', async () => {
subscriptionAsyncIteratorSpy.reset();
resolveAsyncIteratorSpy.reset();
Expand Down

0 comments on commit 2fd7287

Please sign in to comment.