增加基础的连接管理与command调度

This commit is contained in:
白茶清欢 2021-04-09 16:08:00 +08:00
parent 87f0a26e5e
commit d21d9c1395
6 changed files with 53 additions and 21 deletions

View File

@ -26,5 +26,5 @@ type ICommand interface {
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 7:21 下午 2021/3/27 // Date : 7:21 下午 2021/3/27
Execute(ctx *context.WSContext) error Execute(ctx *context.WSContext, data []byte) error
} }

View File

@ -8,10 +8,11 @@
package websocket package websocket
import ( import (
"encoding/json"
"fmt" "fmt"
"sync" "sync"
"github.com/tidwall/gjson"
"github.com/go-developer/websocket/storage" "github.com/go-developer/websocket/storage"
"github.com/go-developer/websocket/context" "github.com/go-developer/websocket/context"
@ -148,20 +149,15 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
// //
// Date : 3:36 下午 2021/3/28 // Date : 3:36 下午 2021/3/28
func dispatchCommand(ctx *context.WSContext, data []byte) error { 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 { if _, exist := commandTable[ctx.Flag]; !exist {
return errors.WithStack(errors.New("未注册【" + ctx.Flag + "】长连接模块")) return errors.WithStack(errors.New("未注册【" + ctx.Flag + "】长连接模块"))
} }
if _, exist := commandTable[ctx.Flag][command.Command]; !exist { cmd := gjson.Get(string(data), "command").String()
return errors.WithStack(errors.New("【" + ctx.Flag + "】长连接模块未注册【" + command.Command + "】指令")) 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() { func run() {

View File

@ -46,7 +46,7 @@ func (e Example) HandshakeURL() []string {
} }
func (e Example) GetCommandList() []abstract.ICommand { func (e Example) GetCommandList() []abstract.ICommand {
return nil return []abstract.ICommand{&exampleCommand{}}
} }
func (e Example) GetModuleFlag() string { func (e Example) GetModuleFlag() string {
@ -56,3 +56,15 @@ func (e Example) GetModuleFlag() string {
func (e Example) GetServerPort() int { func (e Example) GetServerPort() int {
return 10099 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
}

3
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require ( require (
github.com/gin-gonic/gin v1.6.3 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/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.5.1 // indirect github.com/golang/protobuf v1.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // 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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/tidwall/gjson v1.7.4 // indirect
github.com/ugorji/go v1.2.4 // indirect github.com/ugorji/go v1.2.4 // indirect
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect

4
go.sum
View File

@ -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-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 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-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 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 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= 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/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 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w=
github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= 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 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=

View File

@ -18,27 +18,46 @@ var (
) )
func init() { func init() {
Connection = &connection{table: make([]easymap.EasyMap, 0)} c := &connection{}
for i := 0; i < 4096; i++ { c.table, _ = easymap.NewSegment(4096, true)
} Connection = c
} }
type connection struct { type connection struct {
table []easymap.EasyMap table easymap.EasyMap
} }
func (c *connection) Store(ctx *context.WSContext) { func (c *connection) Store(ctx *context.WSContext) {
panic("implement me") c.table.Set(ctx.ConnectionID, ctx)
} }
func (c *connection) Del(ctx *context.WSContext) { func (c *connection) Del(ctx *context.WSContext) {
panic("implement me") c.table.Del(ctx.ConnectionID)
} }
func (c *connection) GetCtxList(cidList ...string) []*context.WSContext { 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) { func (c *connection) Clear(message string) {
panic("implement me") // TODO : 清空连接表
// connectionList := c.GetCtxList()
} }