websocket/storage/connection.go

68 lines
1.5 KiB
Go
Raw Normal View History

// Package storage ...
//
// Description : 在线连接的存储
//
2021-06-05 22:13:09 +08:00
// 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"
)
2021-04-17 23:34:46 +08:00
// NewDefaultConnectionManager 默认的内存连接管理实例
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-04-17 23:34:46 +08:00
//
// Date : 11:32 下午 2021/4/17
func NewDefaultConnectionManager() IConnection {
c := &connection{}
2024-07-22 16:40:33 +08:00
c.table, _ = easymap.NewSegment(4096)
2021-04-17 23:34:46 +08:00
return c
}
type connection struct {
table easymap.EasyMap
}
func (c *connection) Store(ctx *context.WSContext) {
c.table.Set(ctx.ConnectionID, ctx)
}
2021-04-17 23:58:26 +08:00
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()
}