调整接口的逻辑
This commit is contained in:
@@ -84,19 +84,17 @@ func (s *SocketV1) Load(serv *ghttp.Server, prefix string) {
|
|||||||
// @Description:
|
// @Description:
|
||||||
// @receiver s
|
// @receiver s
|
||||||
// @param conn
|
// @param conn
|
||||||
func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
|
func (s *SocketV1) OnConnect(ctx context.Context, ws *websocket.Conn) {
|
||||||
|
|
||||||
defer conn.Close()
|
|
||||||
id := guid.S()
|
id := guid.S()
|
||||||
ip := conn.RemoteAddr().String()
|
ip := ws.RemoteAddr().String()
|
||||||
data := &WebsocketData{
|
conn := &WebsocketData{
|
||||||
Uuid: id,
|
Uuid: id,
|
||||||
Ws: conn,
|
Ws: ws,
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
Groups: make([]string, 0),
|
Groups: make([]string, 0),
|
||||||
RoomId: -1,
|
RoomId: -1,
|
||||||
}
|
}
|
||||||
m.Set(id, data)
|
m.Set(id, conn)
|
||||||
|
|
||||||
//defer delete(Conn, id)
|
//defer delete(Conn, id)
|
||||||
|
|
||||||
@@ -106,22 +104,23 @@ func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
|
|||||||
|
|
||||||
//用户登录钩子执行
|
//用户登录钩子执行
|
||||||
for _, connect := range OnConnectHandlers {
|
for _, connect := range OnConnectHandlers {
|
||||||
connect(data)
|
connect(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
//进入当前连接线程拥堵
|
//进入当前连接线程拥堵
|
||||||
msgType, msg, err := conn.ReadMessage()
|
msgType, msg, err := ws.ReadMessage()
|
||||||
s.Type = msgType
|
s.Type = msgType
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//客户端断开返回错误,断开当前连接
|
//客户端断开返回错误,断开当前连接
|
||||||
//g.Log().Error(ctx, err)
|
//g.Log().Error(ctx, err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
s.OnMessage(m.Get(id).(*WebsocketData), msg, msgType)
|
s.OnMessage(conn, msg, msgType)
|
||||||
}
|
}
|
||||||
//关闭连接触发
|
//关闭连接触发
|
||||||
s.OnClose(data)
|
|
||||||
|
s.OnClose(conn)
|
||||||
g.Log().Debugf(ctx, "断开连接:uuid=%v,ip=%v", id, ip)
|
g.Log().Debugf(ctx, "断开连接:uuid=%v,ip=%v", id, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user