Ping again if stream goes to sleep soon after connection

This commit is contained in:
Tulir Asokan 2020-09-17 18:48:37 +03:00
parent 01101c1272
commit 4b3c411f2f
2 changed files with 31 additions and 6 deletions

17
user.go
View file

@ -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)

View file

@ -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 {