Skip to content

Commit f871c28

Browse files
committed
fix isConnected by triggering a ping
1 parent 2276608 commit f871c28

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

conn.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ It holds all necessary information to make the package work internally.
8080
*/
8181
type Conn struct {
8282
wsConn *websocket.Conn
83+
wsConnOK bool
8384
wsConnMutex sync.RWMutex
8485
session *Session
8586
listener map[string]chan string
@@ -108,6 +109,7 @@ The goroutine for handling incoming messages is started
108109
func NewConn(timeout time.Duration) (*Conn, error) {
109110
wac := &Conn{
110111
wsConn: nil, // will be set in connect()
112+
wsConnOK: false,
111113
wsConnMutex: sync.RWMutex{},
112114
listener: make(map[string]chan string),
113115
listenerMutex: sync.RWMutex{},
@@ -135,7 +137,18 @@ func NewConn(timeout time.Duration) (*Conn, error) {
135137
func (wac *Conn) isConnected() bool {
136138
wac.wsConnMutex.RLock()
137139
defer wac.wsConnMutex.RUnlock()
138-
return wac.wsConn != nil
140+
if wac.wsConn == nil {
141+
return false
142+
}
143+
if wac.wsConnOK {
144+
return true
145+
}
146+
147+
// test connection by sending a ping. This might fail but we ignore that
148+
wac.wsConn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
149+
150+
// this method is expected to be called by loops. So we can just return false
151+
return false
139152
}
140153

141154
// connect should be guarded with wsConnMutex
@@ -152,6 +165,11 @@ func (wac *Conn) connect() error {
152165
return fmt.Errorf("couldn't dial whatsapp web websocket: %v", err)
153166
}
154167

168+
wsConn.SetPongHandler(func(appData string) error {
169+
wac.wsConnOK = true
170+
return nil
171+
})
172+
155173
wsConn.SetCloseHandler(func(code int, text string) error {
156174
fmt.Fprintf(os.Stderr, "websocket connection closed(%d, %s)\n", code, text)
157175

@@ -175,6 +193,7 @@ func (wac *Conn) connect() error {
175193
func (wac *Conn) reconnect() {
176194
wac.wsConnMutex.Lock()
177195
wac.wsConn = nil
196+
wac.wsConnOK = false
178197
wac.wsConnMutex.Unlock()
179198

180199
// wait up to 60 seconds and then reconnect. As writePump should send immediately, it might
@@ -259,6 +278,7 @@ func (wac *Conn) readPump() {
259278
}
260279
msgType, msg, err := wac.wsConn.ReadMessage()
261280
if err != nil {
281+
wac.wsConnOK = false
262282
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
263283
wac.handle(fmt.Errorf("unexpected websocket close: %v", err))
264284
}
@@ -323,6 +343,7 @@ func (wac *Conn) writePump() {
323343
}
324344
if err := wac.wsConn.WriteMessage(msg.messageType, msg.data); err != nil {
325345
fmt.Fprintf(os.Stderr, "error writing to socket: %v\n", err)
346+
wac.wsConnOK = false
326347
// add message to channel again to no loose it
327348
go func() {
328349
wac.writeChan <- msg

0 commit comments

Comments
 (0)