websocket/storage/connection.go

64 lines
1.2 KiB
Go
Raw Normal View History

// 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() {
c := &connection{}
c.table, _ = easymap.NewSegment(4096, true)
Connection = 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) {
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()
}