2021-04-08 18:36:17 +08:00
|
|
|
// Package storage ...
|
|
|
|
//
|
|
|
|
// Description : 在线连接的存储
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-08 18:36:17 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-04-08 5:46 下午
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-developer/gopkg/easymap"
|
|
|
|
"github.com/go-developer/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 {
|
2021-04-09 16:08:00 +08:00
|
|
|
c := &connection{}
|
|
|
|
c.table, _ = easymap.NewSegment(4096, true)
|
2021-04-17 23:34:46 +08:00
|
|
|
return c
|
2021-04-08 18:36:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type connection struct {
|
2021-04-09 16:08:00 +08:00
|
|
|
table easymap.EasyMap
|
2021-04-08 18:36:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) Store(ctx *context.WSContext) {
|
2021-04-09 16:08:00 +08:00
|
|
|
c.table.Set(ctx.ConnectionID, ctx)
|
2021-04-08 18:36:17 +08:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2021-04-09 16:08:00 +08:00
|
|
|
c.table.Del(ctx.ConnectionID)
|
2021-04-08 18:36:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) GetCtxList(cidList ...string) []*context.WSContext {
|
2021-04-09 16:08:00 +08:00
|
|
|
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
|
2021-04-08 18:36:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) Clear(message string) {
|
2021-04-09 16:08:00 +08:00
|
|
|
// TODO : 清空连接表
|
|
|
|
// connectionList := c.GetCtxList()
|
2021-04-08 18:36:17 +08:00
|
|
|
}
|