Skip to content

Commit

Permalink
send ping message on hearbeat for raw websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Aug 22, 2017
1 parent 99cddf1 commit a971f54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 7 additions & 1 deletion sockjs/rawwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ func (w *rawWsReceiver) sendBulk(messages ...string) {
}

func (w *rawWsReceiver) sendFrame(frame string) {
if err := w.conn.WriteMessage(websocket.TextMessage, []byte(frame)); err != nil {
var err error
if frame == "h" {
err = w.conn.WriteMessage(websocket.PingMessage, []byte{})
} else {
err = w.conn.WriteMessage(websocket.TextMessage, []byte(frame))
}
if err != nil {
w.close()
}
}
Expand Down
4 changes: 1 addition & 3 deletions sockjs/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ func (s *session) heartbeat() {
s.Lock()
defer s.Unlock()
if s.recv != nil { // timer could have fired between Lock and timer.Stop in detachReceiver
if !s.raw {
s.recv.sendFrame("h")
}
s.recv.sendFrame("h")
s.timer = time.AfterFunc(s.heartbeatInterval, s.heartbeat)
}
}
Expand Down

0 comments on commit a971f54

Please sign in to comment.