Selaa lähdekoodia

add auto reconnect state

Joyit 2 viikkoa sitten
vanhempi
sitoutus
def30ec802
3 muutettua tiedostoa jossa 34 lisäystä ja 21 poistoa
  1. 1 1
      examples/client-ws2.go
  2. 21 11
      hub.go
  3. 12 9
      line.go

+ 1 - 1
examples/client-ws2.go

@@ -58,7 +58,7 @@ func main() {
 	},
 	)
 
-	err := hub.ConnectToServer(remoteChannel, true, nil)
+	err := hub.ConnectToServer(remoteChannel, true, nil, false)
 	if err != nil {
 		log.Fatalln("[client ConnectToServer ERROR]", err)
 	}

+ 21 - 11
hub.go

@@ -335,7 +335,7 @@ func (h *Hub) sendRequest(gd *GetData) (count int, err error) {
 				log.Println("not channel found")
 				return 0, err
 			}
-			err := h.ConnectToServer(channel, false, nil)
+			err := h.ConnectToServer(channel, false, nil, false)
 			if err != nil {
 				log.Println(err)
 				return 0, err
@@ -645,7 +645,7 @@ func (h *Hub) BindForServer(info *HostInfo) (err error) {
 		})
 		// 新建一个连接
 		if !done {
-			line := NewConnect(h.cf, h, channel, conn, nil)
+			line := NewConnect(h.cf, h, channel, conn, nil, false)
 			h.addLine(line)
 		}
 	}
@@ -663,7 +663,7 @@ func (h *Hub) BindForServer(info *HostInfo) (err error) {
 
 // 新建一个连接,不同的连接协议由底层自己选择
 // channel: 要连接的频道信息,需要能表达频道关键信息的部分
-func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo) (err error) {
+func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo, autoReconnect bool) (err error) {
 	// 检查当前channel是否已经存在
 	if !force {
 		line := h.ChannelToLine(channel)
@@ -682,6 +682,10 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo) (err e
 		if err != nil {
 			return err
 		}
+		if host == nil {
+			// 如果地址为空表示不需要连接,自己返回
+			return
+		}
 	}
 
 	var conn conn.Connect
@@ -818,7 +822,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo) (err e
 	}
 	// 新建一个连接
 	if !done {
-		line := NewConnect(h.cf, h, channel, conn, host)
+		line := NewConnect(h.cf, h, channel, conn, host, autoReconnect)
 		h.addLine(line)
 	}
 	return nil
@@ -833,9 +837,13 @@ func (h *Hub) ConnectToServerX(channel string, force bool, host *HostInfo) {
 			return
 		}
 		host, _ = h.connectHostFunc(channel, Direct)
+		if host == nil {
+			log.Println("not host found for channel:", channel)
+			return
+		}
 	}
 	for {
-		err := h.ConnectToServer(channel, force, host)
+		err := h.ConnectToServer(channel, force, host, true)
 		if err == nil {
 			return
 		}
@@ -866,19 +874,21 @@ func (h *Hub) checkConnect() {
 						log.Println("[proxyTicker connectHostFunc ERROR]", err)
 						return false
 					}
-					err = h.ConnectToServer(line.channel, true, host)
-					if err != nil {
-						log.Println("[checkProxyConnect ConnectToServer WARNING]", err)
+					if host != nil {
+						err = h.ConnectToServer(line.channel, true, host, line.autoReconnect)
+						if err != nil {
+							log.Println("[checkProxyConnect ConnectToServer]", err)
+						}
 					}
 				}
 				return true
 			})
 		case <-connectTicker.C:
 			h.lines.Range(func(id int, line *Line) bool {
-				if line.host != nil && line.state == Disconnected {
-					err := h.ConnectToServer(line.channel, true, nil)
+				if line.autoReconnect && line.host != nil && line.state == Disconnected {
+					err := h.ConnectToServer(line.channel, true, nil, true)
 					if err != nil {
-						log.Println("[connectTicker ConnectToServer WARNING]", err)
+						log.Println("[connectTicker ConnectToServer]", err)
 					}
 				}
 				return true

+ 12 - 9
line.go

@@ -17,11 +17,12 @@ type Line struct {
 	cf             *config.Config
 	hub            *Hub
 	conn           conn.Connect
-	channel        string // 连接对端的频道
-	state          ConnectState
-	host           *HostInfo // 如果有值说明是客户端,就是主动连接端
-	pingID         uint16    // 只有客户端使用
-	pingWrongCount uint8     // 记录 ping id 反馈错误次数,超过3次则重新连接
+	channel        string       // 连接对端的频道
+	state          ConnectState // 连接状态
+	host           *HostInfo    // 如果有值说明是客户端,就是主动连接端
+	autoReconnect  bool         // 指示是否自动重连
+	pingID         uint16       // 只有客户端使用
+	pingWrongCount uint8        // 记录 ping id 反馈错误次数,超过3次则重新连接
 
 	Extra sync.Map // 附加临时信息,由应用端决定具体内容,断线会自动清理
 	Keep  sync.Map // 附加固定信息,由应用端决定具体内容,断线不会自动清理,不过也不能保证一直保持有值,断线后可能会被系统清理
@@ -324,13 +325,15 @@ func NewConnect(
 	channel string,
 	conn conn.Connect,
 	host *HostInfo,
+	autoReconnect bool,
 ) *Line {
 	now := time.Now()
 	cc := &Line{
-		cf:      cf,
-		channel: channel,
-		hub:     hub,
-		pingID:  uint16(now.UnixNano()) % config.ID_MAX,
+		cf:            cf,
+		channel:       channel,
+		hub:           hub,
+		pingID:        uint16(now.UnixNano()) % config.ID_MAX,
+		autoReconnect: autoReconnect,
 
 		sendRequest:  make(chan *RequestData, 32),
 		sendResponse: make(chan *ResponseData, 32),