31 lines
941 B
TypeScript
31 lines
941 B
TypeScript
export default class ConquerWebSocket {
|
|
private webSocket: WebSocket | null = null
|
|
private socketReady = false
|
|
|
|
private getWebSocket(): WebSocket {
|
|
if (this.webSocket !== null && this.socketReady) {
|
|
return this.webSocket
|
|
}
|
|
this.webSocket = new WebSocket(`wss://${window.location.hostname}:${window.location.port}/conquer/websocket`)
|
|
this.webSocket.addEventListener('close', (event) => {
|
|
this.onSocketClose(event)
|
|
})
|
|
this.webSocket.addEventListener('error', (event) => {
|
|
this.onSocketClose(event)
|
|
})
|
|
this.webSocket.addEventListener('open', (event) => {
|
|
this.onSocketOpen(event)
|
|
})
|
|
return this.webSocket
|
|
}
|
|
|
|
private onSocketOpen(event: Event) {
|
|
this.socketReady = true
|
|
}
|
|
|
|
private onSocketClose(event: Event) {
|
|
this.socketReady = false
|
|
console.error(event)
|
|
}
|
|
}
|