|
|
@@ -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
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|