68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
// Package storage ...
|
|
//
|
|
// Description : 在线连接的存储
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2021-04-08 5:46 下午
|
|
package storage
|
|
|
|
import (
|
|
"git.zhangdeman.cn/zhangdeman/gopkg/easymap"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/context"
|
|
)
|
|
|
|
// NewDefaultConnectionManager 默认的内存连接管理实例
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 11:32 下午 2021/4/17
|
|
func NewDefaultConnectionManager() IConnection {
|
|
c := &connection{}
|
|
c.table, _ = easymap.NewSegment(4096, true)
|
|
return c
|
|
}
|
|
|
|
type connection struct {
|
|
table easymap.EasyMap
|
|
}
|
|
|
|
func (c *connection) Store(ctx *context.WSContext) {
|
|
c.table.Set(ctx.ConnectionID, ctx)
|
|
}
|
|
|
|
func (c *connection) Del(ctx *context.WSContext, message string) {
|
|
if len(message) > 0 {
|
|
_ = ctx.Session.Write([]byte(message))
|
|
_ = ctx.Session.Close()
|
|
}
|
|
c.table.Del(ctx.ConnectionID)
|
|
}
|
|
|
|
func (c *connection) GetCtxList(cidList ...string) []*context.WSContext {
|
|
cidTable := make(map[string]bool)
|
|
for _, item := range cidList {
|
|
cidTable[item] = true
|
|
}
|
|
list := c.table.GetAll()
|
|
result := make([]*context.WSContext, 0)
|
|
for _, c := range list {
|
|
if r, ok := c.(*context.WSContext); ok {
|
|
if len(cidTable) == 0 {
|
|
result = append(result, r)
|
|
continue
|
|
}
|
|
if _, exist := cidTable[r.ConnectionID]; exist {
|
|
result = append(result, r)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (c *connection) Clear(message string) {
|
|
// TODO : 清空连接表
|
|
// connectionList := c.GetCtxList()
|
|
}
|