Webatrice websocket refactor (#4435)

* add unit tests for websocket events

* add unit tests for KeepAliveService, clean up keepAlive termination flow

* put keepAlive command in protobuf service and expose thru webClient

* secure wss

* rename files tsx to ts

* add localhost support for ws/wss connection

Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
Jeremy Letto 2021-10-17 19:52:59 -05:00 committed by GitHub
parent f75ff2a7c8
commit 586f23cfa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 568 additions and 77 deletions

View file

@ -17,7 +17,7 @@ export interface WebSocketOptions {
export class WebSocketService {
private socket: WebSocket;
private webClient: WebClient;
public keepAliveService: KeepAliveService;
private keepAliveService: KeepAliveService;
public message$: Subject<MessageEvent> = new Subject();
public statusChange$: Subject<ServerStatus> = new Subject();
@ -36,6 +36,10 @@ export class WebSocketService {
}
public connect(options: WebSocketOptions, protocol: string = 'wss'): void {
if (window.location.hostname === 'localhost') {
protocol = 'ws';
}
const { host, port, keepalive } = options;
this.keepalive = keepalive;
@ -49,7 +53,7 @@ export class WebSocketService {
}
public checkReadyState(state: number): boolean {
return this.socket.readyState === state;
return this.socket?.readyState === state;
}
public send(message): void {
@ -69,13 +73,7 @@ export class WebSocketService {
this.updateStatus(StatusEnum.CONNECTED, "Connected");
this.keepAliveService.startPingLoop(this.keepalive, (pingReceived: Function) => {
const command = this.webClient.protobuf.controller.SessionCommand.create({
".Command_Ping.ext" : this.webClient.protobuf.controller.Command_Ping.create()
});
this.webClient.protobuf.sendSessionCommand(command, () => {
pingReceived();
});
this.webClient.keepAlive(pingReceived);
});
};