优化建立连接

This commit is contained in:
2021-04-09 15:13:22 +08:00
parent 9b826dfae7
commit 87f0a26e5e
3 changed files with 16 additions and 5 deletions

View File

@ -89,10 +89,11 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
ctxInterface, _ := session.Get("ws_context")
ctx := ctxInterface.(*context.WSContext)
ctx.Session = session
if nil != storage.Connection {
storage.Connection.Store(ctx)
if err := wsInstance.Connect(ctx); nil == err {
if nil != storage.Connection {
storage.Connection.Store(ctx)
}
}
wsInstance.Connect(ctx)
})
// 2. 指令处理的函数
ginRouterTable[wsInstance.GetServerPort()].wsServer.HandleMessage(func(session *melody.Session, bytes []byte) {
@ -106,6 +107,9 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
ctxInterface, _ := session.Get("ws_context")
ctx := ctxInterface.(*context.WSContext)
defer func() {
if nil == storage.Connection {
return
}
storage.Connection.Del(ctx)
}()
return wsInstance.Close(ctx, i, s)
@ -114,6 +118,12 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
ginRouterTable[wsInstance.GetServerPort()].wsServer.HandleDisconnect(func(session *melody.Session) {
ctxInterface, _ := session.Get("ws_context")
ctx := ctxInterface.(*context.WSContext)
defer func() {
if nil == storage.Connection {
return
}
storage.Connection.Del(ctx)
}()
wsInstance.Disconnect(ctx)
})
// 注册指令