Skip to content

Commit

Permalink
periodically send a ping message on the WS to avoid proxy auto-termin…
Browse files Browse the repository at this point in the history
…ation of the socket. fixes #221
  • Loading branch information
EricWittmann committed Dec 19, 2017
1 parent 23fe0b9 commit a887a3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public void onMessage(Session session, JsonNode message) {
// TODO send the new content version back to the originating user (with some sort of correlation) so the command can be properly sequenced
editingSession.sendCommandToOthers(session, user, command);
return;
} else if (msgType.equals("ping")) {
logger.debug("PING message received.");
return;
}
logger.error("Unknown message type: {}", msgType);
// TODO something went wrong if we got here - report an error of some kind
Expand Down
32 changes: 30 additions & 2 deletions front-end/app/studio/services/apis-hub.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,32 @@ class ApiEditingSession implements IApiEditingSession {
private _commandHandler: ICommandHandler;
private _oasLibrary: OasLibraryUtils;

private _connected: boolean;
private _pingIntervalId: number;

/**
* Constructor.
* @param {EditableApiDefinition} api
* @param {WebSocket} socket
*/
constructor(private api: EditableApiDefinition, private socket: WebSocket) {
this._oasLibrary = new OasLibraryUtils();
this._connected = false;
}

/**
* Connects the websocket to the server.
* @param {IConnectionHandler} handler
*/
connect(handler: IConnectionHandler): void {
let me: ApiEditingSession = this;
this._connectionHandler = handler;
this.socket.onopen = () => {
console.info("[ApiEditingSession] WS connection to server OPEN.");
this._connected = true;
this._connectionHandler.onConnected();
// Start the 45s ping.
me.ping();
};
this.socket.onmessage = (msgEvent) => {
console.info("[ApiEditingSession] Message received from server.");
Expand All @@ -83,13 +91,14 @@ class ApiEditingSession implements IApiEditingSession {
}
};
this.socket.onclose = (event) => {
// TODO handle unexpected closure here somehow
console.info("[ApiEditingSession] WS connection to server CLOSED: %o", event);
if (event.code === 1000) {
this._connectionHandler.onClosed();
} else {
this._connectionHandler.onDisconnected(event.code);
}
this._connected = false;
window.clearInterval(this._pingIntervalId);
};
}

Expand All @@ -114,13 +123,32 @@ class ApiEditingSession implements IApiEditingSession {
this.socket.send(dataStr);
}

/**
* Called to send a ping message to the server.
*/
sendPing(): void {
console.info("[ApiEditingSession] Sending PING.");
let data: any = {
type: "ping"
};
let dataStr: string = JSON.stringify(data);
this.socket.send(dataStr);
}

/**
* Called to close the connection with the server. Should only be called
* when the user leaves the editor.
*/
close() {
close(): void {
this.socket.close();
}

private ping(): void {
console.info("[ApiEditingSession] Starting the ping interval.");
this._pingIntervalId = window.setInterval(() => {
this.sendPing();
}, 45000);
}
}


Expand Down

0 comments on commit a887a3f

Please # to comment.