优化建立连接
This commit is contained in:
16
construct.go
16
construct.go
@ -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)
|
||||
})
|
||||
// 注册指令
|
||||
|
Reference in New Issue
Block a user