增加建立以及断开连接时,移除连接
This commit is contained in:
parent
cf11a23a9d
commit
9b826dfae7
@ -12,6 +12,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-developer/websocket/storage"
|
||||||
|
|
||||||
"github.com/go-developer/websocket/context"
|
"github.com/go-developer/websocket/context"
|
||||||
|
|
||||||
"gopkg.in/olahol/melody.v1"
|
"gopkg.in/olahol/melody.v1"
|
||||||
@ -87,6 +89,9 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
|
|||||||
ctxInterface, _ := session.Get("ws_context")
|
ctxInterface, _ := session.Get("ws_context")
|
||||||
ctx := ctxInterface.(*context.WSContext)
|
ctx := ctxInterface.(*context.WSContext)
|
||||||
ctx.Session = session
|
ctx.Session = session
|
||||||
|
if nil != storage.Connection {
|
||||||
|
storage.Connection.Store(ctx)
|
||||||
|
}
|
||||||
wsInstance.Connect(ctx)
|
wsInstance.Connect(ctx)
|
||||||
})
|
})
|
||||||
// 2. 指令处理的函数
|
// 2. 指令处理的函数
|
||||||
@ -100,6 +105,9 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
|
|||||||
ginRouterTable[wsInstance.GetServerPort()].wsServer.HandleClose(func(session *melody.Session, i int, s string) error {
|
ginRouterTable[wsInstance.GetServerPort()].wsServer.HandleClose(func(session *melody.Session, i int, s string) error {
|
||||||
ctxInterface, _ := session.Get("ws_context")
|
ctxInterface, _ := session.Get("ws_context")
|
||||||
ctx := ctxInterface.(*context.WSContext)
|
ctx := ctxInterface.(*context.WSContext)
|
||||||
|
defer func() {
|
||||||
|
storage.Connection.Del(ctx)
|
||||||
|
}()
|
||||||
return wsInstance.Close(ctx, i, s)
|
return wsInstance.Close(ctx, i, s)
|
||||||
})
|
})
|
||||||
// 4. 断开连接的处理函数
|
// 4. 断开连接的处理函数
|
||||||
|
@ -10,6 +10,8 @@ package message
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/go-developer/websocket/context"
|
"github.com/go-developer/websocket/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,3 +24,20 @@ func Response(wsCtx *context.WSContext, data map[string]interface{}) error {
|
|||||||
byteData, _ := json.Marshal(data)
|
byteData, _ := json.Marshal(data)
|
||||||
return wsCtx.Session.Write(byteData)
|
return wsCtx.Session.Write(byteData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Broadcast 消息广播
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 5:43 下午 2021/4/8
|
||||||
|
func Broadcast(message string, ctxList ...context.WSContext) map[string]error {
|
||||||
|
result := make(map[string]error)
|
||||||
|
for _, ctx := range ctxList {
|
||||||
|
if nil == ctx.Session {
|
||||||
|
result[ctx.ConnectionID] = errors.New("session is nil")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result[ctx.ConnectionID] = ctx.Session.Write([]byte(message))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
26
storage/IStorage.go
Normal file
26
storage/IStorage.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Package storage ...
|
||||||
|
//
|
||||||
|
// Description : 各种内存数据的管理,基于接口,可自己重新实现管理实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2021-04-08 5:29 下午
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import "github.com/go-developer/websocket/context"
|
||||||
|
|
||||||
|
// IConnection 连接管理接口定义
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 5:30 下午 2021/4/8
|
||||||
|
type IConnection interface {
|
||||||
|
// Store 存储
|
||||||
|
Store(ctx *context.WSContext)
|
||||||
|
// Del 移除
|
||||||
|
Del(ctx *context.WSContext)
|
||||||
|
// GetCtxList 获取连接列表
|
||||||
|
GetCtxList(cidList ...string) []*context.WSContext
|
||||||
|
// Clear 清空连接
|
||||||
|
Clear(message string)
|
||||||
|
}
|
44
storage/connection.go
Normal file
44
storage/connection.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// Package storage ...
|
||||||
|
//
|
||||||
|
// Description : 在线连接的存储
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2021-04-08 5:46 下午
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-developer/gopkg/easymap"
|
||||||
|
"github.com/go-developer/websocket/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Connection 连接管理
|
||||||
|
Connection IConnection
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Connection = &connection{table: make([]easymap.EasyMap, 0)}
|
||||||
|
for i := 0; i < 4096; i++ {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type connection struct {
|
||||||
|
table []easymap.EasyMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) Store(ctx *context.WSContext) {
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) Del(ctx *context.WSContext) {
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) GetCtxList(cidList ...string) []*context.WSContext {
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) Clear(message string) {
|
||||||
|
panic("implement me")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user