You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

92 lines
2.2 KiB

/**
* @Author: Aceld
* @Date: 2019/4/30 17:42
* @Mail: danbing.at@gmail.com
* ZinxV0.11测试,测试Zinx 日志模块功能 zlog模块
*/
package main
import (
"zinx/ziface"
"zinx/zlog"
"zinx/znet"
)
//ping test 自定义路由
type PingRouter struct {
znet.BaseRouter
}
//Ping Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
zlog.Debug("Call PingRouter Handle")
//先读取客户端的数据,再回写ping...ping...ping
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping"))
if err != nil {
zlog.Error(err)
}
}
type HelloZinxRouter struct {
znet.BaseRouter
}
//HelloZinxRouter Handle
func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
zlog.Debug("Call HelloZinxRouter Handle")
//先读取客户端的数据,再回写ping...ping...ping
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.10"))
if err != nil {
zlog.Error(err)
}
}
//创建连接的时候执行
func DoConnectionBegin(conn ziface.IConnection) {
zlog.Debug("DoConnecionBegin is Called ... ")
//设置两个链接属性,在连接创建之后
zlog.Debug("Set conn Name, Home done!")
conn.SetProperty("Name", "Aceld")
conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
zlog.Error(err)
}
}
//连接断开的时候执行
func DoConnectionLost(conn ziface.IConnection) {
//在连接销毁之前,查询conn的Name,Home属性
if name, err:= conn.GetProperty("Name"); err == nil {
zlog.Error("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
zlog.Error("Conn Property Home = ", home)
}
zlog.Debug("DoConneciotnLost is Called ... ")
}
func main() {
//创建一个server句柄
s := znet.NewServer()
//注册链接hook回调函数
s.SetOnConnStart(DoConnectionBegin)
s.SetOnConnStop(DoConnectionLost)
//配置路由
s.AddRouter(0, &PingRouter{})
s.AddRouter(1, &HelloZinxRouter{})
//开启服务
s.Serve()
}