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

Commit

Permalink
use legacy keep alive message as needed (#297)
Browse files Browse the repository at this point in the history
don't send keep alive until the connection is established
  • Loading branch information
NeoPhi authored Dec 1, 2017
1 parent e03f2b8 commit 7482425
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dist/test
src
src
4 changes: 4 additions & 0 deletions src/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default class MessageTypes {
* @deprecated
*/
public static INIT_FAIL = 'init_fail';
/**
* @deprecated
*/
public static KEEP_ALIVE = 'keepalive';

constructor() {
throw new Error('Static Class');
Expand Down
34 changes: 17 additions & 17 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,6 @@ export class SubscriptionServer {
connectionContext.socket = socket;
connectionContext.operations = {};

// Regular keep alive messages if keepAlive is set
if (this.keepAlive) {
const keepAliveTimer = setInterval(() => {
if (socket.readyState === WebSocket.OPEN) {
this.sendMessage(connectionContext, undefined, MessageTypes.GQL_CONNECTION_KEEP_ALIVE, undefined);
} else {
clearInterval(keepAliveTimer);
}
}, this.keepAlive);
}

const connectionClosedHandler = (error: any) => {
if (error) {
this.sendError(
Expand Down Expand Up @@ -282,12 +271,15 @@ export class SubscriptionServer {
);

if (this.keepAlive) {
this.sendMessage(
connectionContext,
undefined,
MessageTypes.GQL_CONNECTION_KEEP_ALIVE,
undefined,
);
this.sendKeepAlive(connectionContext);
// Regular keep alive messages if keepAlive is set
const keepAliveTimer = setInterval(() => {
if (connectionContext.socket.readyState === WebSocket.OPEN) {
this.sendKeepAlive(connectionContext);
} else {
clearInterval(keepAliveTimer);
}
}, this.keepAlive);
}
}).catch((error: Error) => {
this.sendError(
Expand Down Expand Up @@ -461,6 +453,14 @@ export class SubscriptionServer {
};
}

private sendKeepAlive(connectionContext: ConnectionContext): void {
if (connectionContext.isLegacy) {
this.sendMessage(connectionContext, undefined, MessageTypes.KEEP_ALIVE, undefined);
} else {
this.sendMessage(connectionContext, undefined, MessageTypes.GQL_CONNECTION_KEEP_ALIVE, undefined);
}
}

private sendMessage(connectionContext: ConnectionContext, opId: string, type: string, payload: any): void {
const parsedMessage = parseLegacyProtocolMessage(connectionContext, {
type,
Expand Down
29 changes: 28 additions & 1 deletion src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1941,12 +1941,39 @@ describe('Server', function () {
const parsedMessage = JSON.parse(message.data);
if (parsedMessage.type === MessageTypes.GQL_CONNECTION_KEEP_ALIVE) {
yieldCount += 1;
if (yieldCount > 1) {
if (yieldCount > 2) {
client.close();
done();
}
}
};
client.onopen = () => {
client.send(JSON.stringify({
id: 1,
type: MessageTypes.GQL_CONNECTION_INIT,
}));
};
});

it('sends legacy keep alive signal in the socket', function (done) {
let client = new WebSocket(`ws://localhost:${KEEP_ALIVE_TEST_PORT}/`, GRAPHQL_SUBSCRIPTIONS);
let yieldCount = 0;
client.onmessage = (message: any) => {
const parsedMessage = JSON.parse(message.data);
if (parsedMessage.type === MessageTypes.KEEP_ALIVE) {
yieldCount += 1;
if (yieldCount > 2) {
client.close();
done();
}
}
};
client.onopen = () => {
client.send(JSON.stringify({
id: 1,
type: MessageTypes.INIT,
}));
};
});
});

Expand Down

0 comments on commit 7482425

Please sign in to comment.