Skip to content

Commit

Permalink
add callbal BeforeRedial in WS interface
Browse files Browse the repository at this point in the history
  • Loading branch information
irai committed Dec 10, 2020
1 parent df54008 commit 708dfdb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func (wsConn *WSConn) clientReaderLoop(process func(clientId string, msg WSMsg)
if err != ErrorClosed && !wsConn.closing {
log.WithFields(log.Fields{"clientID": wsConn.ClientId}).Error("WS client failed to read websocket message", err)
if AutoRedial {
// wsConn.c.Close() // close the underlying WS; this will stop ping goroutine.
wsConn.callback.BeforeRedial()

wsConn.redialLoop()

// Run this call back in a goroutine because THIS reader loop must be running to receive responses.
Expand Down
6 changes: 5 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ func (h TestClientWSHandler) Closed(wsConn *WSConn) {
log.Infof("WS client %s nclosed %d", wsConn.ClientId, countClosed)
}

func (h TestClientWSHandler) AfterRedial(wsConn *WSConn) {
func (h TestClientWSHandler) BeforeRedial() {
countRedial++
log.Infof("WS client before redial count=%d", countRedial)
}

func (h TestClientWSHandler) AfterRedial(wsConn *WSConn) {
log.Infof("WS client after redial %s count %d", wsConn.ClientId, countRedial)
}

Expand Down
5 changes: 4 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ func (h TestServerWSHandler) Closed(wsConn *WSConn) {
log.Infof("server conn %s closed nconns %d", wsConn.ClientId, countConnections)
}

func (h TestServerWSHandler) AfterRedial(wsConn *WSConn) {
func (h TestServerWSHandler) BeforeRedial() {
h.countRedial++
log.Infof("WS client redial %v", h.countRedial)
}
func (h TestServerWSHandler) AfterRedial(wsConn *WSConn) {
log.Infof("WS client %s redial %d", wsConn.ClientId, h.countRedial)
}

Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type WSServer interface {
}

type WSClient interface {
BeforeRedial()
AfterRedial(wsConn *WSConn)
Process(clientId string, msg WSMsg) (response WSMsg, err error)
Closed(wsConn *WSConn)
Expand Down

0 comments on commit 708dfdb

Please sign in to comment.