package main import ( "fmt" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" ) //ping test 自定义路由 type PingRouter struct { znet.BaseRouter } //Ping Handle func (this *PingRouter) Handle(request ziface.IRequest) { fmt.Println("Call PingRouter Handle") //先读取客户端的数据,再回写ping...ping...ping fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData())) err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping")) if err != nil { fmt.Println(err) } } type HelloZinxRouter struct { znet.BaseRouter } //HelloZinxRouter Handle func (this *HelloZinxRouter) Handle(request ziface.IRequest) { fmt.Println("Call HelloZinxRouter Handle") //先读取客户端的数据,再回写ping...ping...ping fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData())) err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.8")) if err != nil { fmt.Println(err) } } //创建连接的时候执行 func DoConnectionBegin(conn ziface.IConnection) { fmt.Println("DoConnecionBegin is Called ... ") err := conn.SendMsg(2, []byte("DoConnection BEGIN...")) if err != nil { fmt.Println(err) } } //连接断开的时候执行 func DoConnectionLost(conn ziface.IConnection) { fmt.Println("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() }