增加基础的连接管理与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

@ -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() {