Browse Source

fixed bug.

zhou.xiaoning 2 năm trước cách đây
mục cha
commit
6f7270809e
2 tập tin đã thay đổi với 19 bổ sung5 xóa
  1. 15 1
      client/client.go
  2. 4 4
      quote_publish/quote_publish.go

+ 15 - 1
client/client.go

@@ -10,6 +10,7 @@ import (
 	"mtp20access/packet"
 	"mtp20access/publish"
 	"sync"
+	"time"
 
 	"github.com/gorilla/websocket"
 	"github.com/mitchellh/mapstructure"
@@ -141,13 +142,18 @@ func (r *Client) SetWebSocket(ws *websocket.Conn) (err error) {
 		r.wsConn.Close()
 	}
 	r.wsConn = ws
+	r.writeChan = make(chan []byte, 100)
 
 	// 开始读取客户端发送信息
 	go r.readClientWsMessage()
 	// 开始推送客户端信息循环
-	r.writeChan = make(chan []byte, 100)
 	go r.writeClientWsMessage()
 
+	r.wsConn.SetCloseHandler(func(code int, text string) error {
+		close(r.wsCloseChan)
+		return nil
+	})
+
 	return
 }
 
@@ -155,6 +161,9 @@ func (r *Client) SetWebSocket(ws *websocket.Conn) (err error) {
 // 注意: 阻塞式, 直到websocket关闭才退出
 func (r *Client) readClientWsMessage() {
 	for {
+		// 40秒心跳超时
+		r.wsConn.SetReadDeadline(time.Now().Add(40 * time.Second))
+
 		mt, msg, err := r.wsConn.ReadMessage()
 		if err != nil {
 			fmt.Println(err)
@@ -174,6 +183,11 @@ func (r *Client) readClientWsMessage() {
 			fmt.Println(string(msg))
 			_ = r.wsConn.WriteMessage(mt, msg)
 		}
+
+		select {
+		case <-r.wsCloseChan:
+			return
+		}
 	}
 }
 

+ 4 - 4
quote_publish/quote_publish.go

@@ -40,10 +40,10 @@ func (r *QuotePublish) connQuotePublish() {
 
 	var err error
 	for {
-		if r.conn != nil {
-			r.conn.Close()
-			close(r.closeCh)
-		}
+		// if r.conn != nil {
+		// 	r.conn.Close()
+		// 	close(r.closeCh)
+		// }
 
 		r.setStatus(2)
 		global.M2A_LOG.Info("正在尝试连接行情发布服务", zap.Any("host", global.M2A_CONFIG.System.QuotePublishAddr))