This commit is contained in:
laodaming 2023-07-26 17:39:56 +08:00
parent 0142e04ef5
commit 7280c38b16

View File

@ -81,16 +81,16 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
defer conn.Close() defer conn.Close()
rsp := types.DataTransferData{} rsp := types.DataTransferData{}
//鉴权不成功10秒后断开 //鉴权不成功10秒后断开
if !l.checkAuth(svcCtx, r) { /*if !l.checkAuth(svcCtx, r) {
rsp.T = constants.WEBSOCKET_UNAUTH rsp.T = constants.WEBSOCKET_UNAUTH
rsp.D = "unAuth" rsp.D = "unAuth"
b, _ := json.Marshal(rsp) b, _ := json.Marshal(rsp)
//先发一条正常信息
_ = conn.WriteMessage(websocket.TextMessage, b) _ = conn.WriteMessage(websocket.TextMessage, b)
select { //发送关闭信息
case <-time.After(time.Second * 10): //10秒后断开 _ = conn.WriteMessage(websocket.CloseMessage, nil)
return return
} }*/
}
//生成连接唯一标识 //生成连接唯一标识
flag := uuid.New().String() + time.Now().Format("20060102150405") flag := uuid.New().String() + time.Now().Format("20060102150405")
ws := wsConnectItem{ ws := wsConnectItem{
@ -147,11 +147,6 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques
// 心跳 // 心跳
func (w *wsConnectItem) heartbeat() { func (w *wsConnectItem) heartbeat() {
rsp := types.DataTransferData{
T: constants.WEBSOCKET_HEARTBEAT,
D: "",
}
b, _ := json.Marshal(rsp)
for { for {
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
select { select {
@ -159,7 +154,7 @@ func (w *wsConnectItem) heartbeat() {
return return
default: default:
//发送心跳信息 //发送心跳信息
if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil {
logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) logx.Error("发送心跳信息异常,关闭连接:", w.flag, err)
w.close() w.close()
return return
@ -173,6 +168,8 @@ func (w *wsConnectItem) close() {
w.mutex.Lock() w.mutex.Lock()
defer w.mutex.Unlock() defer w.mutex.Unlock()
logx.Info("websocket:", w.flag, " is closing...") logx.Info("websocket:", w.flag, " is closing...")
//发送关闭信息
_ = w.conn.WriteMessage(websocket.CloseMessage, nil)
w.conn.Close() w.conn.Close()
mapConnPool.Delete(w.flag) mapConnPool.Delete(w.flag)
if !w.isClose { if !w.isClose {