Skip to content

Commit

Permalink
client auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed May 23, 2016
1 parent 088c6d4 commit a3f82e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions network/tcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type TCPClient struct {
ConnNum int
ConnectInterval time.Duration
PendingWriteNum int
AutoReconnect bool
NewAgent func(*TCPConn) Agent
conns ConnSet
wg sync.WaitGroup
Expand Down Expand Up @@ -84,6 +85,7 @@ func (client *TCPClient) dial() net.Conn {
func (client *TCPClient) connect() {
defer client.wg.Done()

reconnect:
conn := client.dial()
if conn == nil {
return
Expand All @@ -108,6 +110,10 @@ func (client *TCPClient) connect() {
delete(client.conns, conn)
client.Unlock()
agent.OnClose()

if client.AutoReconnect {
goto reconnect
}
}

func (client *TCPClient) Close() {
Expand Down
6 changes: 6 additions & 0 deletions network/ws_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type WSClient struct {
PendingWriteNum int
MaxMsgLen uint32
HandshakeTimeout time.Duration
AutoReconnect bool
NewAgent func(*WSConn) Agent
dialer websocket.Dialer
conns WebsocketConnSet
Expand Down Expand Up @@ -85,6 +86,7 @@ func (client *WSClient) dial() *websocket.Conn {
func (client *WSClient) connect() {
defer client.wg.Done()

reconnect:
conn := client.dial()
if conn == nil {
return
Expand All @@ -110,6 +112,10 @@ func (client *WSClient) connect() {
delete(client.conns, conn)
client.Unlock()
agent.OnClose()

if client.AutoReconnect {
goto reconnect
}
}

func (client *WSClient) Close() {
Expand Down

0 comments on commit a3f82e4

Please sign in to comment.