mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
refactor websocket layer
This commit is contained in:
parent
4f68190a25
commit
f0c3581d26
9 changed files with 87 additions and 142 deletions
|
|
@ -14,7 +14,6 @@ export interface WebSocketServiceConfig {
|
|||
|
||||
export class WebSocketService {
|
||||
private socket: WebSocket;
|
||||
private testSocket: WebSocket;
|
||||
|
||||
private config: WebSocketServiceConfig;
|
||||
private response: IWebClientResponse;
|
||||
|
|
@ -29,7 +28,7 @@ export class WebSocketService {
|
|||
this.config = config;
|
||||
this.response = config.response;
|
||||
|
||||
this.keepAliveService = new KeepAliveService(this);
|
||||
this.keepAliveService = new KeepAliveService(() => this.checkReadyState(WebSocket.OPEN));
|
||||
this.keepAliveService.disconnected$.subscribe(() => {
|
||||
this.disconnect();
|
||||
this.config.onStatusChange(App.StatusEnum.DISCONNECTED, 'Connection timeout');
|
||||
|
|
@ -47,16 +46,6 @@ export class WebSocketService {
|
|||
this.socket = this.createWebSocket(`${protocol}://${host}:${port}`);
|
||||
}
|
||||
|
||||
public testConnect(options: Enriched.WebSocketConnectOptions, protocol: string = 'wss'): void {
|
||||
if (window.location.hostname === 'localhost') {
|
||||
protocol = 'ws';
|
||||
}
|
||||
|
||||
const { host, port } = options;
|
||||
|
||||
this.testWebSocket(`${protocol}://${host}:${port}`);
|
||||
}
|
||||
|
||||
public disconnect(): void {
|
||||
if (this.socket) {
|
||||
this.socket.close();
|
||||
|
|
@ -109,31 +98,4 @@ export class WebSocketService {
|
|||
return socket;
|
||||
}
|
||||
|
||||
private testWebSocket(url: string): void {
|
||||
if (this.testSocket) {
|
||||
this.testSocket.onerror = null;
|
||||
this.testSocket.close();
|
||||
}
|
||||
|
||||
const socket = new WebSocket(url);
|
||||
socket.binaryType = 'arraybuffer';
|
||||
|
||||
const connectionTimer = setTimeout(() => socket.close(), CLIENT_OPTIONS.keepalive);
|
||||
|
||||
socket.onopen = () => {
|
||||
clearTimeout(connectionTimer);
|
||||
this.response.session.testConnectionSuccessful();
|
||||
socket.close();
|
||||
};
|
||||
|
||||
socket.onerror = () => {
|
||||
this.response.session.testConnectionFailed();
|
||||
};
|
||||
|
||||
socket.onclose = () => {
|
||||
this.testSocket = null;
|
||||
}
|
||||
|
||||
this.testSocket = socket;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue