📲 Reconnect socket after connection closes
This commit is contained in:
@@ -17,10 +17,12 @@ type WebsocketOutData = string | ArrayBufferLike | Blob | ArrayBufferView;
|
|||||||
|
|
||||||
class SocketService {
|
class SocketService {
|
||||||
private socket!: WebSocket;
|
private socket!: WebSocket;
|
||||||
|
private url?:string
|
||||||
|
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
public connect(url: string): void {
|
public connect(url: string): void {
|
||||||
|
this.url = url
|
||||||
this.socket = new WebSocket(url);
|
this.socket = new WebSocket(url);
|
||||||
this.socket.binaryType = 'arraybuffer';
|
this.socket.binaryType = 'arraybuffer';
|
||||||
this.socket.onopen = () => this.handleConnected();
|
this.socket.onopen = () => this.handleConnected();
|
||||||
@@ -50,6 +52,7 @@ class SocketService {
|
|||||||
|
|
||||||
private handleDisconnected(): void {
|
private handleDisconnected(): void {
|
||||||
isConnected.set(false);
|
isConnected.set(false);
|
||||||
|
setTimeout(() => this.connect(this.url as string), 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
private getJsonFromMessage(msg: string): Result<WebSocketJsonMsg, string> {
|
private getJsonFromMessage(msg: string): Result<WebSocketJsonMsg, string> {
|
||||||
|
|||||||
Reference in New Issue
Block a user