From 9b826dfae71b824832bdb39771fe7a6cbeb635c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Thu, 8 Apr 2021 18:36:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BB=BA=E7=AB=8B=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5=E6=97=B6,?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- construct.go | 8 ++++++++ message/message.go | 19 +++++++++++++++++++ storage/IStorage.go | 26 +++++++++++++++++++++++++ storage/connection.go | 44 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 storage/IStorage.go create mode 100644 storage/connection.go diff --git a/construct.go b/construct.go index e124ce4..fdae93d 100644 --- a/construct.go +++ b/construct.go @@ -12,6 +12,8 @@ import ( "fmt" "sync" + "github.com/go-developer/websocket/storage" + "github.com/go-developer/websocket/context" "gopkg.in/olahol/melody.v1" @@ -87,6 +89,9 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { ctxInterface, _ := session.Get("ws_context") ctx := ctxInterface.(*context.WSContext) ctx.Session = session + if nil != storage.Connection { + storage.Connection.Store(ctx) + } wsInstance.Connect(ctx) }) // 2. 指令处理的函数 @@ -100,6 +105,9 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { ginRouterTable[wsInstance.GetServerPort()].wsServer.HandleClose(func(session *melody.Session, i int, s string) error { ctxInterface, _ := session.Get("ws_context") ctx := ctxInterface.(*context.WSContext) + defer func() { + storage.Connection.Del(ctx) + }() return wsInstance.Close(ctx, i, s) }) // 4. 断开连接的处理函数 diff --git a/message/message.go b/message/message.go index 6698fe9..3228bbd 100644 --- a/message/message.go +++ b/message/message.go @@ -10,6 +10,8 @@ package message import ( "encoding/json" + "github.com/pkg/errors" + "github.com/go-developer/websocket/context" ) @@ -22,3 +24,20 @@ func Response(wsCtx *context.WSContext, data map[string]interface{}) error { byteData, _ := json.Marshal(data) return wsCtx.Session.Write(byteData) } + +// Broadcast 消息广播 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:43 下午 2021/4/8 +func Broadcast(message string, ctxList ...context.WSContext) map[string]error { + result := make(map[string]error) + for _, ctx := range ctxList { + if nil == ctx.Session { + result[ctx.ConnectionID] = errors.New("session is nil") + continue + } + result[ctx.ConnectionID] = ctx.Session.Write([]byte(message)) + } + return result +} diff --git a/storage/IStorage.go b/storage/IStorage.go new file mode 100644 index 0000000..df29e81 --- /dev/null +++ b/storage/IStorage.go @@ -0,0 +1,26 @@ +// Package storage ... +// +// Description : 各种内存数据的管理,基于接口,可自己重新实现管理实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-04-08 5:29 下午 +package storage + +import "github.com/go-developer/websocket/context" + +// IConnection 连接管理接口定义 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:30 下午 2021/4/8 +type IConnection interface { + // Store 存储 + Store(ctx *context.WSContext) + // Del 移除 + Del(ctx *context.WSContext) + // GetCtxList 获取连接列表 + GetCtxList(cidList ...string) []*context.WSContext + // Clear 清空连接 + Clear(message string) +} diff --git a/storage/connection.go b/storage/connection.go new file mode 100644 index 0000000..d01b229 --- /dev/null +++ b/storage/connection.go @@ -0,0 +1,44 @@ +// 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() { + Connection = &connection{table: make([]easymap.EasyMap, 0)} + for i := 0; i < 4096; i++ { + } +} + +type connection struct { + table []easymap.EasyMap +} + +func (c *connection) Store(ctx *context.WSContext) { + panic("implement me") +} + +func (c *connection) Del(ctx *context.WSContext) { + panic("implement me") +} + +func (c *connection) GetCtxList(cidList ...string) []*context.WSContext { + panic("implement me") +} + +func (c *connection) Clear(message string) { + panic("implement me") +}