Skip to content

Commit

Permalink
feat: Add ping/pong calls
Browse files Browse the repository at this point in the history
  • Loading branch information
yeskiy committed Jan 23, 2023
1 parent 545ca07 commit 695d106
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = class RPCClient {
this.options.versionAcceptType
) &&
clientRequest.data.functionResolver ===
"WeakFunctionPool"
this.options.universalRPC.FunctionResolver.name()
)
) {
throw new RuntimeError(
Expand Down Expand Up @@ -79,6 +79,17 @@ module.exports = class RPCClient {
`Server ready message received`
);

this.websocket.addEventListener("pong", () => {
this.logger.silly(`Pong received`);
setTimeout(() => {
this.websocket.ping();
this.logger.silly(`Ping sent`);
}, 500);
});

this.websocket.ping();
this.logger.silly(`Initial Ping sent`);

this.websocket.sessionId = this.key;
const universalSession = new UniversalRPC(
this.websocket,
Expand Down
4 changes: 4 additions & 0 deletions src/functionResolvers/weakFunctionPool.functionResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ module.exports = class WeakFunctionPool extends FunctionResolver {
this.logger.silly("WFP initialized");
}

static name() {
return "WeakFunctionPool";
}

// eslint-disable-next-line class-methods-use-this
messageBuilder = (type, requestId, data) => ({
type,
Expand Down
9 changes: 8 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ module.exports = class RPCServer extends EventEmitter {
);
this.logger.silly("Removed init message listener");

websocketConnection.addEventListener("ping", () => {
this.logger.silly("Ping Received");
websocketConnection.pong();
this.logger.silly("Pong sent");
});
this.logger.silly("Added ping listener");

websocketConnection.send({
type: "ready",
data: { key },
Expand Down Expand Up @@ -137,7 +144,7 @@ module.exports = class RPCServer extends EventEmitter {
const initPayload = {
key,
version: this.options.version,
functionResolver: this.options.universalRPC.FunctionResolver.name,
functionResolver: this.options.universalRPC.FunctionResolver.name(),
};
websocketConnection.send({ type: "init", data: initPayload });
this.logger.verbose(
Expand Down

0 comments on commit 695d106

Please sign in to comment.