From d21d9c13957585701b673fb17c0fe7945aff0ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Fri, 9 Apr 2021 16:08:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9F=BA=E7=A1=80=E7=9A=84?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=AE=A1=E7=90=86=E4=B8=8Ecommand=E8=B0=83?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/ICommand.go | 2 +- construct.go | 16 ++++++---------- example/server.go | 14 +++++++++++++- go.mod | 3 ++- go.sum | 4 ++++ storage/connection.go | 35 +++++++++++++++++++++++++++-------- 6 files changed, 53 insertions(+), 21 deletions(-) diff --git a/abstract/ICommand.go b/abstract/ICommand.go index 51d8bef..a69552f 100644 --- a/abstract/ICommand.go +++ b/abstract/ICommand.go @@ -26,5 +26,5 @@ type ICommand interface { // Author : go_developer@163.com<张德满> // // Date : 7:21 下午 2021/3/27 - Execute(ctx *context.WSContext) error + Execute(ctx *context.WSContext, data []byte) error } diff --git a/construct.go b/construct.go index c7f3880..7c96a8b 100644 --- a/construct.go +++ b/construct.go @@ -8,10 +8,11 @@ package websocket import ( - "encoding/json" "fmt" "sync" + "github.com/tidwall/gjson" + "github.com/go-developer/websocket/storage" "github.com/go-developer/websocket/context" @@ -148,20 +149,15 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { // // Date : 3:36 下午 2021/3/28 func dispatchCommand(ctx *context.WSContext, data []byte) error { - var command struct { - Command string `json:"command"` - } - if err := json.Unmarshal(data, &command); nil != err { - return err - } if _, exist := commandTable[ctx.Flag]; !exist { return errors.WithStack(errors.New("未注册【" + ctx.Flag + "】长连接模块")) } - if _, exist := commandTable[ctx.Flag][command.Command]; !exist { - return errors.WithStack(errors.New("【" + ctx.Flag + "】长连接模块未注册【" + command.Command + "】指令")) + cmd := gjson.Get(string(data), "command").String() + if _, exist := commandTable[ctx.Flag][cmd]; !exist { + return errors.WithStack(errors.New("【" + ctx.Flag + "】长连接模块未注册【" + cmd + "】指令")) } - return nil + return commandTable[ctx.Flag][cmd].Execute(ctx, data) } func run() { diff --git a/example/server.go b/example/server.go index dc31a50..89ed017 100644 --- a/example/server.go +++ b/example/server.go @@ -46,7 +46,7 @@ func (e Example) HandshakeURL() []string { } func (e Example) GetCommandList() []abstract.ICommand { - return nil + return []abstract.ICommand{&exampleCommand{}} } func (e Example) GetModuleFlag() string { @@ -56,3 +56,15 @@ func (e Example) GetModuleFlag() string { func (e Example) GetServerPort() int { return 10099 } + +type exampleCommand struct { +} + +func (e exampleCommand) GetCommand() string { + return "ping" +} + +func (e exampleCommand) Execute(ctx *context.WSContext, data []byte) error { + message.Response(ctx, map[string]interface{}{"ping": "pong"}) + return nil +} diff --git a/go.mod b/go.mod index 9449c07..3f3b3bd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/gin-gonic/gin v1.6.3 - github.com/go-developer/gopkg v0.0.0-20210401121730-91894a6f9659 + github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/golang/protobuf v1.5.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect @@ -13,6 +13,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/pkg/errors v0.9.1 + github.com/tidwall/gjson v1.7.4 // indirect github.com/ugorji/go v1.2.4 // indirect gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 3ee314b..0264da5 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/go-developer/gopkg v0.0.0-20210326100134-08a8a8015cc5 h1:Id0MV9wihSMC github.com/go-developer/gopkg v0.0.0-20210326100134-08a8a8015cc5/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= github.com/go-developer/gopkg v0.0.0-20210401121730-91894a6f9659 h1:wHI+HmfpLeG9B6EL47jLxNl4zB5dSW3ihtlWxvRrDzk= github.com/go-developer/gopkg v0.0.0-20210401121730-91894a6f9659/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= +github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c h1:WotMdy0sk6f9+kMxzqBbdzlZtok7ieiTHnn1eVeqTTE= +github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= @@ -120,6 +122,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tidwall/gjson v1.6.8 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w= github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= +github.com/tidwall/gjson v1.7.4 h1:19cchw8FOxkG5mdLRkGf9jqIqEyqdZhPqW60XfyFxk8= +github.com/tidwall/gjson v1.7.4/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk= github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= diff --git a/storage/connection.go b/storage/connection.go index d01b229..c504fdb 100644 --- a/storage/connection.go +++ b/storage/connection.go @@ -18,27 +18,46 @@ var ( ) func init() { - Connection = &connection{table: make([]easymap.EasyMap, 0)} - for i := 0; i < 4096; i++ { - } + c := &connection{} + c.table, _ = easymap.NewSegment(4096, true) + Connection = c } type connection struct { - table []easymap.EasyMap + table easymap.EasyMap } func (c *connection) Store(ctx *context.WSContext) { - panic("implement me") + c.table.Set(ctx.ConnectionID, ctx) } func (c *connection) Del(ctx *context.WSContext) { - panic("implement me") + c.table.Del(ctx.ConnectionID) } func (c *connection) GetCtxList(cidList ...string) []*context.WSContext { - panic("implement me") + 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) { - panic("implement me") + // TODO : 清空连接表 + // connectionList := c.GetCtxList() }