diff --git a/abstract/ICommand.go b/abstract/ICommand.go index a69552f..5fe757e 100644 --- a/abstract/ICommand.go +++ b/abstract/ICommand.go @@ -7,7 +7,10 @@ // Date : 2021-03-27 6:34 下午 package abstract -import "github.com/go-developer/websocket/context" +import ( + "github.com/go-developer/websocket/config" + "github.com/go-developer/websocket/context" +) // ICommand 指令 // @@ -21,6 +24,12 @@ type ICommand interface { // // Date : 7:21 下午 2021/3/27 GetCommand() string + // GetConfigOption 获取指令的配置 + // + // Author : go_developer@163.com<张德满> + // + // Date : 2:49 下午 2021/4/17 + GetConfigOption() []config.SetCommandConfig // Execute 执行指令的逻辑 // // Author : go_developer@163.com<张德满> diff --git a/config/command.go b/config/command.go new file mode 100644 index 0000000..992b4f2 --- /dev/null +++ b/config/command.go @@ -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 +} diff --git a/config/server.go b/config/server.go new file mode 100644 index 0000000..6af5615 --- /dev/null +++ b/config/server.go @@ -0,0 +1,8 @@ +// Package config ... +// +// Description : WS-Server 相关配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-04-17 2:32 下午 +package config diff --git a/construct.go b/construct.go index 7c96a8b..9a27273 100644 --- a/construct.go +++ b/construct.go @@ -11,6 +11,10 @@ import ( "fmt" "sync" + "github.com/go-developer/websocket/message" + + "github.com/go-developer/websocket/config" + "github.com/tidwall/gjson" "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() - 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 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() { diff --git a/example/server.go b/example/server.go index 89ed017..8704358 100644 --- a/example/server.go +++ b/example/server.go @@ -10,6 +10,8 @@ package main import ( "fmt" + "github.com/go-developer/websocket/config" + "github.com/go-developer/websocket/message" "github.com/go-developer/websocket" @@ -64,6 +66,10 @@ func (e exampleCommand) GetCommand() string { return "ping" } +func (e exampleCommand) GetConfigOption() []config.SetCommandConfig { + return []config.SetCommandConfig{config.ClosePushCommandErrorMessage()} +} + func (e exampleCommand) Execute(ctx *context.WSContext, data []byte) error { message.Response(ctx, map[string]interface{}{"ping": "pong"}) return nil