Pārlūkot izejas kodu

Ping again if stream goes to sleep soon after connection

Tulir Asokan 4 gadi atpakaļ
vecāks
revīzija
4b3c411f2f
2 mainītis faili ar 31 papildinājumiem un 6 dzēšanām
  1. 17 0
      user.go
  2. 14 6
      whatsapp-ext/stream.go

+ 17 - 0
user.go

@@ -62,6 +62,7 @@ type User struct {
 
 	cleanDisconnection  bool
 	batteryWarningsSent int
+	lastReconnection    int64
 
 	chatListReceived chan struct{}
 	syncPortalsDone  chan struct{}
@@ -480,6 +481,7 @@ func (user *User) postConnPing() bool {
 
 func (user *User) intPostLogin() {
 	defer user.syncWait.Done()
+	user.lastReconnection = time.Now().Unix()
 	user.createCommunity()
 	user.tryAutomaticDoublePuppeting()
 
@@ -502,6 +504,21 @@ func (user *User) intPostLogin() {
 	}
 }
 
+func (user *User) HandleStreamEvent(evt whatsappExt.StreamEvent) {
+	if evt.Type == whatsappExt.StreamSleep {
+		if user.lastReconnection+60 > time.Now().Unix() {
+			user.lastReconnection = 0
+			user.log.Infoln("Stream went to sleep soon after reconnection, making new post-connection ping in 20 seconds")
+			go func() {
+				time.Sleep(20 * time.Second)
+				user.postConnPing()
+			}()
+		}
+	} else {
+		user.log.Infofln("Stream event: %+v", evt)
+	}
+}
+
 func (user *User) HandleChatList(chats []whatsapp.Chat) {
 	user.log.Infoln("Chat list received")
 	chatMap := make(map[string]whatsapp.Chat)

+ 14 - 6
whatsapp-ext/stream.go

@@ -30,9 +30,12 @@ const (
 )
 
 type StreamEvent struct {
-	Type    StreamType
-	Boolean bool
-	Version string
+	Type StreamType
+
+	IsOutdated bool
+	Version    string
+
+	Extra []json.RawMessage
 }
 
 type StreamEventHandler interface {
@@ -48,9 +51,14 @@ func (ext *ExtendedConn) handleMessageStream(message []json.RawMessage) {
 		return
 	}
 
-	if event.Type == StreamUpdate && len(message) > 4 {
-		json.Unmarshal(message[1], event.Boolean)
-		json.Unmarshal(message[2], event.Version)
+	if event.Type == StreamUpdate && len(message) >= 3 {
+		_ = json.Unmarshal(message[1], &event.IsOutdated)
+		_ = json.Unmarshal(message[2], &event.Version)
+		if len(message) >= 4 {
+			event.Extra = message[3:]
+		}
+	} else if len(message) >= 2 {
+		event.Extra = message[1:]
 	}
 
 	for _, handler := range ext.handlers {