websocket/storage/connection.go
zhangdeman001 99774ac513 更新依赖包版本
Author:    白茶清欢 <go_developer@163.com>
2022-06-27 16:25:17 +08:00

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/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()
}