xingo之添加自定义telnet命令

1)xingo单机模式开启telnet在线调试服务

      修改配置文件conf/server.json,添加配置项:

      “WriteList”: [“127.0.0.1”] //白名单

      “DebugPort”: 8881 //telnet端口

      xingo自带的命令如下:

      

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
-------welcome to xingo telnet tool(node: single_mode)---------
help
有关某个命令的详细信息,请键入 help 命令名
pprofcpu:
----------- start: 开始收集服务cpu占用信息
----------- stop:  结束数据收集
----------- profiling: 分析(goroutine, heap, thread, block)

2)xingo分布式模式开启telnet在线调试服务

     修改配置文件conf/server.json,添加配置项:

      “WriteList”[“127.0.0.1”] //白名单

     修改配置文件conf/clusterconf.json,添加配置项:

      “DebugPort”8881 //telnet端口

      xingo自带的命令如下:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
-------welcome to xingo telnet tool(node: master)---------
help
有关某个命令的详细信息,请键入 help 命令名
pprofcpu:
----------- start: 开始收集服务cpu占用信息
----------- stop:  结束数据收集
----------- profiling: 分析(goroutine, heap, thread, block)
closeserver:
----------- all delay: 延迟delay秒时间关闭所有子节点
----------- node name delay:  延迟delay秒时间关闭指定节点

reloadcfg:
----------- all delay: 延迟delay秒时间重新加载所有节点的配置文件
----------- node name delay:  延迟delay秒时间重新加载指定节点
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
-------welcome to xingo telnet tool(node: net1)---------
help
有关某个命令的详细信息,请键入 help 命令名
pprofcpu:
----------- start: 开始收集服务cpu占用信息
----------- stop:  结束数据收集
----------- profiling: 分析(goroutine, heap, thread, block)

3)实现自己的telnet在线调试命令(获取服务器的在线玩家数)

只需要实现接口ICommand

type ICommand interface {
	Run([]string) string
	Help() string
	Name() string
}
package cmd

import (
	"fmt"
	"xingo_demo/core"
)

type OnlineCommand struct {
}

func NewOnlineCommand() *OnlineCommand{
	return &OnlineCommand{}
}
func (this *OnlineCommand)Name()string{
	return "online"
}

func (this *OnlineCommand)Help()string{
	return fmt.Sprintf("online:\r\n" +
		"----------- login: 登陆玩家数\r\n" +
		"----------- nologin:  未登陆玩家数\r\n" +
		"----------- kick [userId]:  踢出玩家")
}

func (this *OnlineCommand)Run(args []string) string{
	if len(args) == 0{
		return this.Help()
	}else{
		switch args[0] {
		case "login":
			core.WorldMgrObj.RLock()
			ss := len(core.WorldMgrObj.Players)
			core.WorldMgrObj.RUnlock()
			return fmt.Sprintf("login player: %d", ss)
		default:
			return "未实现"
		}
	}
	return "OK"
}

4)注册命令到xingo

utils.GlobalObject.CmdInterpreter.AddCommand(cmd.NewOnlineCommand())

5)编译运行xingo server

2017/08/16 21:50:43 safetimer.go 99 [info [xingo safe timer scheduelloop runing.]]
2017/08/16 21:50:43 xingo.go 28 [debug [telnet tool start: 0.0.0.0:8881.]]
2017/08/16 21:50:43 cmdinterpreter.go 25 [debug [add command  pprofcpu]]
2017/08/16 21:50:43 cmdinterpreter.go 25 [debug [add command  online]]
2017/08/16 21:50:43 server.go 125 [info [AddRouter]]
2017/08/16 21:50:43 msghandle.go 77 [info [add api 0]]
2017/08/16 21:50:43 server.go 125 [info [AddRouter]]
2017/08/16 21:50:43 msghandle.go 77 [info [add api 2]]
2017/08/16 21:50:43 server.go 125 [info [AddRouter]]
2017/08/16 21:50:43 msghandle.go 77 [info [add api 3]]
2017/08/16 21:50:43 server.go 93 [info [start xingo server telnet_server...]]
2017/08/16 21:50:43 server.go 93 [info [start xingo server single_mode...]]
2017/08/16 21:50:43 msghandle.go 95 [info [init thread mode workpool.]]

6)telnet 127.0.0.1 8881

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
-------welcome to xingo telnet tool(node: single_mode)---------
help
有关某个命令的详细信息,请键入 help 命令名
online:
----------- login: 登陆玩家数
----------- nologin:  未登陆玩家数
----------- kick [userId]:  踢出玩家
pprofcpu:
----------- start: 开始收集服务cpu占用信息
----------- stop:  结束数据收集
----------- profiling: 分析(goroutine, heap, thread, block)

7)大功告成