diff --git a/cluster/gate/gate.go b/cluster/gate/gate.go index 465842d..f3c07d3 100644 --- a/cluster/gate/gate.go +++ b/cluster/gate/gate.go @@ -86,7 +86,9 @@ func (g *Gate) Init() { func (g *Gate) Start() { g.startNetworkServer() - g.startWsNetworkServer() + if g.opts.wsServerEnable { + g.startWsNetworkServer() + } g.startRPCServer() @@ -107,7 +109,9 @@ func (g *Gate) Destroy() { g.stopNetworkServer() - g.stopWsNetworkServer() + if g.opts.wsServerEnable { + g.stopWsNetworkServer() + } g.stopRPCServer() diff --git a/cluster/gate/options.go b/cluster/gate/options.go index c124686..0ddb024 100644 --- a/cluster/gate/options.go +++ b/cluster/gate/options.go @@ -25,54 +25,58 @@ import ( ) const ( - defaultName = "gate" // 默认名称 - defaultTimeout = 30 * time.Second // 默认超时时间 - defaultCodec = "proto" // 默认编解码器名称 - defaultWorkloadStat = 500 - defaultNamespace = "" + defaultName = "gate" // 默认名称 + defaultTimeout = 30 * time.Second // 默认超时时间 + defaultCodec = "proto" // 默认编解码器名称 + defaultWorkloadStat = 500 + defaultNamespace = "" + defaultWsServerEnable = false ) const ( - defaultIDKey = "config.cluster.gate.id" - defaultNameKey = "config.cluster.gate.name" - defaultTimeoutKey = "config.cluster.gate.timeout" - defaultCodecKey = "config.cluster.gate.codec" - defaultDecryptorKey = "config.cluster.gate.decryptor" - defaultEncryptorKey = "config.cluster.node.encryptor" - defaultWorkloadStatKey = "config.workload.stat" - defaultNamespaceKey = "config.cluster.gate.namespace" + defaultIDKey = "config.cluster.gate.id" + defaultNameKey = "config.cluster.gate.name" + defaultTimeoutKey = "config.cluster.gate.timeout" + defaultCodecKey = "config.cluster.gate.codec" + defaultDecryptorKey = "config.cluster.gate.decryptor" + defaultEncryptorKey = "config.cluster.node.encryptor" + defaultWorkloadStatKey = "config.workload.stat" + defaultNamespaceKey = "config.cluster.gate.namespace" + defaultWsServerEnableKey = "config.network.ws.enable" ) type Option func(o *options) type options struct { - id string // 实例ID - name string // 实例名称 - ctx context.Context // 上下文 - timeout time.Duration // RPC调用超时时间 - server network.Server // 网关服务器 - wsServer network.Server // websocket 网关服务器 - locator locate.Locator // 用户定位器 - registry registry.Registry // 服务注册器 - codec encoding.Codec // 编解码器 - transporter transport.Transporter // 消息传输器 - promServer prometheus.PromServer // 埋点采集服务器 - catServer *cat.Server - receiveHook []hook.ReceiveHook // 接受消息hook - encryptor crypto.Encryptor // 消息加密器 - decryptor crypto.Decryptor - workloadStat int32 - namespace string + id string // 实例ID + name string // 实例名称 + ctx context.Context // 上下文 + timeout time.Duration // RPC调用超时时间 + server network.Server // 网关服务器 + wsServer network.Server // websocket 网关服务器 + locator locate.Locator // 用户定位器 + registry registry.Registry // 服务注册器 + codec encoding.Codec // 编解码器 + transporter transport.Transporter // 消息传输器 + promServer prometheus.PromServer // 埋点采集服务器 + catServer *cat.Server + receiveHook []hook.ReceiveHook // 接受消息hook + encryptor crypto.Encryptor // 消息加密器 + decryptor crypto.Decryptor + workloadStat int32 + namespace string + wsServerEnable bool } func defaultOptions() *options { opts := &options{ - ctx: context.Background(), - name: defaultName, - timeout: defaultTimeout, - codec: encoding.Invoke(defaultCodec), - workloadStat: config.Get(defaultWorkloadStatKey, defaultWorkloadStat).Int32(), - namespace: config.Get(defaultNamespaceKey, defaultNamespace).String(), + ctx: context.Background(), + name: defaultName, + timeout: defaultTimeout, + codec: encoding.Invoke(defaultCodec), + workloadStat: config.Get(defaultWorkloadStatKey, defaultWorkloadStat).Int32(), + namespace: config.Get(defaultNamespaceKey, defaultNamespace).String(), + wsServerEnable: config.Get(defaultWsServerEnableKey, defaultWsServerEnable).Bool(), } if id := config.Get(defaultIDKey).String(); id != "" { @@ -126,9 +130,13 @@ func WithServer(server network.Server) Option { // WithWsServer 设置websocket 服务器 func WithWsServer(server network.Server) Option { - return func(o *options) { - o.wsServer = server + wsServerEnable := config.Get(defaultWsServerEnableKey, defaultWsServerEnable).Bool() + if wsServerEnable { + return func(o *options) { + o.wsServer = server + } } + return func(o *options) {} } // WithTimeout 设置RPC调用超时时间