Skip to content

Commit 117540e

Browse files
committed
client: add method for reconnecting socket
1 parent ccfa04f commit 117540e

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

client.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type Client struct {
6969

7070
isLoggedIn atomic.Bool
7171
expectedDisconnect *exsync.Event
72+
forceAutoReconnect atomic.Bool
7273
EnableAutoReconnect bool
7374
InitialAutoReconnect bool
7475
LastSuccessfulConnect time.Time
@@ -538,7 +539,7 @@ func (cli *Client) onDisconnect(ctx context.Context, ns *socket.NoiseSocket, rem
538539
if cli.socket == ns {
539540
cli.socket = nil
540541
cli.clearResponseWaiters(xmlStreamEndNode)
541-
if !cli.isExpectedDisconnect() && remote {
542+
if !cli.isExpectedDisconnect() && (cli.forceAutoReconnect.Swap(false) || remote) {
542543
cli.Log.Debugf("Emitting Disconnected event")
543544
go cli.dispatchEvent(&events.Disconnected{})
544545
go cli.autoReconnect(ctx)
@@ -553,10 +554,12 @@ func (cli *Client) onDisconnect(ctx context.Context, ns *socket.NoiseSocket, rem
553554
}
554555

555556
func (cli *Client) expectDisconnect() {
557+
cli.forceAutoReconnect.Store(false)
556558
cli.expectedDisconnect.Set()
557559
}
558560

559561
func (cli *Client) resetExpectedDisconnect() {
562+
cli.forceAutoReconnect.Store(false)
560563
cli.expectedDisconnect.Clear()
561564
}
562565

@@ -626,6 +629,18 @@ func (cli *Client) Disconnect() {
626629
cli.clearDelayedMessageRequests()
627630
}
628631

632+
// ResetConnection disconnects from the WhatsApp web websocket and forces an automatic reconnection.
633+
// This will not do anything if the socket is already disconnected or if EnableAutoReconnect is false.
634+
func (cli *Client) ResetConnection() {
635+
if cli == nil {
636+
return
637+
}
638+
cli.socketLock.Lock()
639+
cli.forceAutoReconnect.Store(true)
640+
cli.unlockedDisconnect()
641+
cli.socketLock.Unlock()
642+
}
643+
629644
// Disconnect closes the websocket connection.
630645
func (cli *Client) unlockedDisconnect() {
631646
if cli.socket != nil {

0 commit comments

Comments
 (0)