- tcp协议改为websocket
- 单/双命令配置 DoubleMsgID
- 单(1001)/主(1)子(1001)命令切换
- 丰富的demo
- 新增心跳检测功能
- 若用到mysql需要安装驱动
gorm.io/driver/mysql
- 心跳该为时间轮检测
看云-《zinx框架教程-基于Golang的轻量级并发服务器》
简书-《zinx框架教程-基于Golang的轻量级并发服务器》
基于zinx框架开发的服务器应用,主函数步骤比较精简,最多只需要3步即可。
funcmain() {
//1 创建一个server句柄,内含初始化 Glog...server:=znet.NewServer()
//2 配置路由server.AddRouter(1, &api.PingRouter{})
//3 开启服务bindAddress:=fmt.Sprintf("%s:%d", global.Object.Host, global.Object.TCPPort)
router:=gin.Default()
router.GET("/", server.Serve)
router.Run(bindAddress)
}其中(api.PingRouter)自定义路由及业务处理: 代码跳转
zinx的消息处理采用,[MsgLength]|[MsgID]|[Data]的封包格式
代码跳转
{
"Name": "zinx-websocket Demo",
"Host": "127.0.0.1",
"TcpPort": 8999,
"MaxConn": 3,
"WorkerPoolSize": 10,
"LogFile": "zinx.log",
"HeartbeatTime": 60
}funcNewServer () ziface.IServer创建一个zinx服务器句柄,该句柄作为当前服务器应用程序的主枢纽,包括如下功能:
func (s*Server) Start(c*gin.Context)func (s*Server) Stop()func (s*Server) Serve(c*gin.Context)func (s*Server) AddRouter (msgIduint16, router ziface.IRouter) func (s*Server) SetOnConnStart(hookFuncfunc (ziface.IConnection))func (s*Server) SetOnConnStop(hookFuncfunc (ziface.IConnection))//实现router时,先嵌入这个基类,然后根据需要对这个基类的方法进行重写typeBaseRouterstruct {}
//这里之所以BaseRouter的方法都为空,// 是因为有的Router不希望有PreHandle或PostHandle// 所以Router全部继承BaseRouter的好处是,不需要实现PreHandle和PostHandle也可以实例化func (br*BaseRouter)PreHandle(req ziface.IRequest){}
func (br*BaseRouter)Handle(req ziface.IRequest){}
func (br*BaseRouter)PostHandle(req ziface.IRequest){}func (c*Connection) GetTCPConnection() *net.TCPConnfunc (c*Connection) GetConnID() uint32func (c*Connection) RemoteAddr() net.Addrfunc (c*Connection) SendMsg(msgIDuint32, msgTypeint, data []byte) errorfunc (c*Connection) SendBuffMsg(msgIDuint32, msgTypeint, data []byte) error//默认二进制消息func (c*Connection) SendBinaryMsg(msgIDuint32, data []byte) errorfunc (c*Connection) SendBinaryBuffMsg(msgIDuint32, data []byte) error//设置链接属性func (c*Connection) SetProperty(keystring, valueinterface{})
//获取链接属性func (c*Connection) GetProperty(keystring) (interface{}, error)
//移除链接属性func (c*Connection) RemoveProperty(keystring)