refactor: update reconnect code for better understanding
This commit is contained in:
parent
c4c787a782
commit
342ecde6bb
2 changed files with 9 additions and 13 deletions
|
@ -1,9 +1,10 @@
|
|||
package websocket
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
)
|
||||
|
||||
|
|
|
@ -22,27 +22,22 @@ func (c *Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
|||
c.Conn.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||
_, message, err := c.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
c.LastErr = err
|
||||
|
||||
// On normal closure exit gracefully
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||
return
|
||||
}
|
||||
|
||||
c.LastErr = err
|
||||
// Try to reconnect on unexpected disconnect
|
||||
for {
|
||||
channel <- serverMessage // send last known ServerMessage to update view
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// reconnect while preserving password
|
||||
// reset connection and reconnect
|
||||
c.Conn = nil
|
||||
pw := c.Password
|
||||
for !c.Connected() {
|
||||
channel <- serverMessage // send last known ServerMessage to update view
|
||||
time.Sleep(time.Second)
|
||||
*c = Connect(c.Server)
|
||||
}
|
||||
c.Password = pw
|
||||
|
||||
if c.Connected() {
|
||||
break
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue