ICommand增加配置方法,支持覆盖全局配置
This commit is contained in:
parent
18e1719e86
commit
c26624befb
@ -7,7 +7,10 @@
|
|||||||
// Date : 2021-03-27 6:34 下午
|
// Date : 2021-03-27 6:34 下午
|
||||||
package abstract
|
package abstract
|
||||||
|
|
||||||
import "github.com/go-developer/websocket/context"
|
import (
|
||||||
|
"github.com/go-developer/websocket/config"
|
||||||
|
"github.com/go-developer/websocket/context"
|
||||||
|
)
|
||||||
|
|
||||||
// ICommand 指令
|
// ICommand 指令
|
||||||
//
|
//
|
||||||
@ -21,6 +24,12 @@ type ICommand interface {
|
|||||||
//
|
//
|
||||||
// Date : 7:21 下午 2021/3/27
|
// Date : 7:21 下午 2021/3/27
|
||||||
GetCommand() string
|
GetCommand() string
|
||||||
|
// GetConfigOption 获取指令的配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2:49 下午 2021/4/17
|
||||||
|
GetConfigOption() []config.SetCommandConfig
|
||||||
// Execute 执行指令的逻辑
|
// Execute 执行指令的逻辑
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<张德满>
|
// Author : go_developer@163.com<张德满>
|
||||||
|
46
config/command.go
Normal file
46
config/command.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// Package config ...
|
||||||
|
//
|
||||||
|
// Description : WS-Command 相关配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2021-04-17 2:34 下午
|
||||||
|
package config
|
||||||
|
|
||||||
|
// commandConfig 指令相关配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2:36 下午 2021/4/17
|
||||||
|
type CommandConfig struct {
|
||||||
|
PushMessageWithError bool // 当调度指令时,指令执行错误,是否想客户端推送错误消息
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCommandConfig 设置command配置
|
||||||
|
type SetCommandConfig func(cc *CommandConfig)
|
||||||
|
|
||||||
|
// ClosePushCommandErrorMessage 关闭指令执行异常时的消息推送
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2:51 下午 2021/4/17
|
||||||
|
func ClosePushCommandErrorMessage() SetCommandConfig {
|
||||||
|
return func(cc *CommandConfig) {
|
||||||
|
cc.PushMessageWithError = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCommandConfig 指令的配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2:39 下午 2021/4/17
|
||||||
|
func NewCommandConfig(optionFunc ...SetCommandConfig) *CommandConfig {
|
||||||
|
cc := &CommandConfig{
|
||||||
|
PushMessageWithError: true, // 默认推送异常消息
|
||||||
|
}
|
||||||
|
for _, o := range optionFunc {
|
||||||
|
o(cc)
|
||||||
|
}
|
||||||
|
return cc
|
||||||
|
}
|
8
config/server.go
Normal file
8
config/server.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// Package config ...
|
||||||
|
//
|
||||||
|
// Description : WS-Server 相关配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 2021-04-17 2:32 下午
|
||||||
|
package config
|
29
construct.go
29
construct.go
@ -11,6 +11,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-developer/websocket/message"
|
||||||
|
|
||||||
|
"github.com/go-developer/websocket/config"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/go-developer/websocket/storage"
|
"github.com/go-developer/websocket/storage"
|
||||||
@ -154,10 +158,31 @@ func dispatchCommand(ctx *context.WSContext, data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := gjson.Get(string(data), "command").String()
|
cmd := gjson.Get(string(data), "command").String()
|
||||||
if _, exist := commandTable[ctx.Flag][cmd]; !exist {
|
var (
|
||||||
|
exist bool
|
||||||
|
cmdInstance abstract.ICommand
|
||||||
|
cmdConfig *config.CommandConfig
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if cmdInstance, exist = commandTable[ctx.Flag][cmd]; !exist {
|
||||||
return errors.WithStack(errors.New("【" + ctx.Flag + "】长连接模块未注册【" + cmd + "】指令"))
|
return errors.WithStack(errors.New("【" + ctx.Flag + "】长连接模块未注册【" + cmd + "】指令"))
|
||||||
}
|
}
|
||||||
return commandTable[ctx.Flag][cmd].Execute(ctx, data)
|
optionList := cmdInstance.GetConfigOption()
|
||||||
|
if nil == optionList {
|
||||||
|
optionList = make([]config.SetCommandConfig, 0)
|
||||||
|
}
|
||||||
|
cmdConfig = config.NewCommandConfig(optionList...)
|
||||||
|
if err = cmdInstance.Execute(ctx, data); nil != err {
|
||||||
|
if cmdConfig.PushMessageWithError {
|
||||||
|
_ = message.Response(ctx, map[string]interface{}{
|
||||||
|
"command": cmd,
|
||||||
|
"message": err.Error(),
|
||||||
|
"success": false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() {
|
func run() {
|
||||||
|
@ -10,6 +10,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-developer/websocket/config"
|
||||||
|
|
||||||
"github.com/go-developer/websocket/message"
|
"github.com/go-developer/websocket/message"
|
||||||
|
|
||||||
"github.com/go-developer/websocket"
|
"github.com/go-developer/websocket"
|
||||||
@ -64,6 +66,10 @@ func (e exampleCommand) GetCommand() string {
|
|||||||
return "ping"
|
return "ping"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e exampleCommand) GetConfigOption() []config.SetCommandConfig {
|
||||||
|
return []config.SetCommandConfig{config.ClosePushCommandErrorMessage()}
|
||||||
|
}
|
||||||
|
|
||||||
func (e exampleCommand) Execute(ctx *context.WSContext, data []byte) error {
|
func (e exampleCommand) Execute(ctx *context.WSContext, data []byte) error {
|
||||||
message.Response(ctx, map[string]interface{}{"ping": "pong"})
|
message.Response(ctx, map[string]interface{}{"ping": "pong"})
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user