Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: trigger reconnect on emit if not connected #162

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/event_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class EventHub {
* @return {Boolean}
*/
isConnected(): boolean {
return this._socketIo?.isConnected() || false;
return this._socketIo?.socket.connected || false;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions source/simple_socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ export default class SimpleSocketIOClient {
}
this.reconnecting = false;
this.reconnectionAttempts = 0; // Reset reconnection attempts
this.handleEvent("connect", {});
this.flushPacketQueue();
// Set connected property to true
this.socket.connected = true;
this.handleEvent("connect", {});
this.flushPacketQueue();
}
/**
* Handles WebSocket closing
Expand Down Expand Up @@ -313,11 +313,14 @@ export default class SimpleSocketIOClient {
const dataString = eventData ? `:::${JSON.stringify(payload)}` : "";
const packet = `${PACKET_TYPES.event}${dataString}`;

if (this.webSocket?.readyState === WebSocket.OPEN) {
if (this.isConnected()) {
this.webSocket.send(packet);
} else {
this.packetQueue.push(packet);
}
if (this.webSocket && !this.socket.connected && !this.reconnecting) {
this.reconnect();
}
}
/**
* Send the queued packets to the server
Expand Down