@@ -371,6 +371,9 @@ func (cli *Client) closeSocketWaitChan() {
371
371
}
372
372
373
373
func (cli * Client ) getOwnID () types.JID {
374
+ if cli == nil {
375
+ return types .EmptyJID
376
+ }
374
377
id := cli .Store .ID
375
378
if id == nil {
376
379
return types .EmptyJID
@@ -379,6 +382,9 @@ func (cli *Client) getOwnID() types.JID {
379
382
}
380
383
381
384
func (cli * Client ) WaitForConnection (timeout time.Duration ) bool {
385
+ if cli == nil {
386
+ return false
387
+ }
382
388
timeoutChan := time .After (timeout )
383
389
cli .socketLock .RLock ()
384
390
for cli .socket == nil || ! cli .socket .IsConnected () || ! cli .IsLoggedIn () {
@@ -398,6 +404,9 @@ func (cli *Client) WaitForConnection(timeout time.Duration) bool {
398
404
// Connect connects the client to the WhatsApp web websocket. After connection, it will either
399
405
// authenticate if there's data in the device store, or emit a QREvent to set up a new link.
400
406
func (cli * Client ) Connect () error {
407
+ if cli == nil {
408
+ return ErrClientIsNil
409
+ }
401
410
cli .socketLock .Lock ()
402
411
defer cli .socketLock .Unlock ()
403
412
if cli .socket != nil {
@@ -444,7 +453,7 @@ func (cli *Client) Connect() error {
444
453
445
454
// IsLoggedIn returns true after the client is successfully connected and authenticated on WhatsApp.
446
455
func (cli * Client ) IsLoggedIn () bool {
447
- return cli .isLoggedIn .Load ()
456
+ return cli != nil && cli .isLoggedIn .Load ()
448
457
}
449
458
450
459
func (cli * Client ) onDisconnect (ns * socket.NoiseSocket , remote bool ) {
@@ -508,6 +517,9 @@ func (cli *Client) autoReconnect() {
508
517
// IsConnected checks if the client is connected to the WhatsApp web websocket.
509
518
// Note that this doesn't check if the client is authenticated. See the IsLoggedIn field for that.
510
519
func (cli * Client ) IsConnected () bool {
520
+ if cli == nil {
521
+ return false
522
+ }
511
523
cli .socketLock .RLock ()
512
524
connected := cli .socket != nil && cli .socket .IsConnected ()
513
525
cli .socketLock .RUnlock ()
@@ -519,7 +531,7 @@ func (cli *Client) IsConnected() bool {
519
531
// This will not emit any events, the Disconnected event is only used when the
520
532
// connection is closed by the server or a network error.
521
533
func (cli * Client ) Disconnect () {
522
- if cli .socket == nil {
534
+ if cli == nil || cli .socket == nil {
523
535
return
524
536
}
525
537
cli .socketLock .Lock ()
@@ -544,7 +556,9 @@ func (cli *Client) unlockedDisconnect() {
544
556
// Note that this will not emit any events. The LoggedOut event is only used for external logouts
545
557
// (triggered by the user from the main device or by WhatsApp servers).
546
558
func (cli * Client ) Logout () error {
547
- if cli .MessengerConfig != nil {
559
+ if cli == nil {
560
+ return ErrClientIsNil
561
+ } else if cli .MessengerConfig != nil {
548
562
return errors .New ("can't logout with Messenger credentials" )
549
563
}
550
564
ownID := cli .getOwnID ()
@@ -728,6 +742,9 @@ func (cli *Client) handlerQueueLoop(ctx context.Context) {
728
742
}
729
743
730
744
func (cli * Client ) sendNodeAndGetData (node waBinary.Node ) ([]byte , error ) {
745
+ if cli == nil {
746
+ return nil , ErrClientIsNil
747
+ }
731
748
cli .socketLock .RLock ()
732
749
sock := cli .socket
733
750
cli .socketLock .RUnlock ()
0 commit comments