refactor: manual reconnect
This commit is contained in:
parent
a7f1372df1
commit
c6023edef7
|
@ -12,12 +12,25 @@ class SocketClient extends decorate(class {}) {
|
|||
|
||||
constructor(address, options = {}) {
|
||||
super();
|
||||
this.address = address;
|
||||
this.isConnected = false;
|
||||
this.socket = io(address, {
|
||||
this.options = {
|
||||
parser: SocketIoParser,
|
||||
path: '/avocado',
|
||||
perMessageDeflate: false,
|
||||
...options,
|
||||
reconnection: false,
|
||||
...options
|
||||
};
|
||||
this.socket = null;
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
if (this.socket) {
|
||||
this.socket.destroy();
|
||||
}
|
||||
this.socket = io(this.address, {
|
||||
...this.options,
|
||||
});
|
||||
this.socket.on('connect', () => {
|
||||
this.isConnected = true;
|
||||
|
@ -26,6 +39,16 @@ class SocketClient extends decorate(class {}) {
|
|||
this.socket.on('disconnect', () => {
|
||||
this.isConnected = false;
|
||||
this.emit('disconnect');
|
||||
let backoff = 100;
|
||||
const tryToReconnect = () => {
|
||||
if (this.isConnected) {
|
||||
return;
|
||||
}
|
||||
this.connect();
|
||||
backoff = Math.min(2000, backoff * 1.5);
|
||||
setTimeout(tryToReconnect, backoff);
|
||||
}
|
||||
setTimeout(tryToReconnect, backoff);
|
||||
});
|
||||
for (const Packet of allPackets()) {
|
||||
const id = idFromPacket(Packet);
|
||||
|
@ -35,6 +58,10 @@ class SocketClient extends decorate(class {}) {
|
|||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.socket.disconnect();
|
||||
}
|
||||
|
||||
send(packet) {
|
||||
const id = idFromPacket(packet.constructor);
|
||||
this.socket.emit(id, packet.data);
|
||||
|
|
Loading…
Reference in New Issue
Block a user