1 Star 0 Fork 0

杨程显 / empty_zinx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Server.go 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
SLAKM 提交于 2023-07-25 22:59 . [fix]:仍然使用msgID作为解析
package main
import (
"fmt"
"gitee.com/yang-chengxian/empty_zinx/src/ziface"
"gitee.com/yang-chengxian/empty_zinx/src/znet"
)
/*
基于Zinx开发的应用程序
*/
// PingRouter ping test 自定义路由
type PingRouter struct {
znet.BaseRouter
}
// Handle Test Handle
func (pr *PingRouter) Handle(request ziface.IRequest) {
fmt.Println("Call ping Handle...")
// 先读取客户端的数据, 再回写ping...ping..ping
fmt.Println("recv from client: msgID = ", request.GetMessageID(),
",Data = ", string(request.GetMessageBody()))
err := request.GetConnection().SendMsg(200, []byte("ping...ping...ping"))
if err != nil {
fmt.Println(err)
}
}
// HelloEmpty hello test 自定义路由
type HelloEmpty struct {
znet.BaseRouter
}
// Handle Test Handle
func (pr *HelloEmpty) Handle(request ziface.IRequest) {
fmt.Println("Call Hello Handle...")
// 先读取客户端的数据, 再回写ping...ping..ping
fmt.Println("recv from client: msgID = ", request.GetMessageID(),
",Data = ", string(request.GetMessageBody()))
err := request.GetConnection().SendMsg(201, []byte("Hello Welcome to Empty"))
if err != nil {
fmt.Println(err)
}
}
// DoConnectionBegin 创建连接之后的钩子函数
func DoConnectionBegin(conn ziface.IConnection) {
fmt.Println("====> DoConnectionBegin is called")
err := conn.SendMsg(202, []byte("DoConnection Begin"))
if err != nil {
fmt.Println(err)
}
// 给当前的连接设置一些属性
fmt.Println("Set conn Properties")
conn.SetProperty("Name", "SLAKM")
conn.SetProperty("Poe", "https://poe.com/Assistant")
}
// DoConnectionClose 创建连接之后的钩子函数
func DoConnectionClose(conn ziface.IConnection) {
fmt.Println("====> DoConnectionClose is called")
fmt.Println("conn ID =", conn.GetConnID())
// 获取连接属性
name, err := conn.GetProperty("Name")
if err != nil {
fmt.Println(err)
}
poe, err := conn.GetProperty("Poe")
if err != nil {
fmt.Println(err)
}
fmt.Println("conn Name =", name)
fmt.Println("conn Poe =", poe)
}
func main() {
// 创建一个Server句柄, 使用Zinx的api
s := znet.NewServer("src/demo/ZinxV0.9/config/zinx.json")
// 注册连接Hook钩子函数
s.SetOnConnStart(DoConnectionBegin)
s.SetOnConnClose(DoConnectionClose)
// 给当前zinx框架添加一个自定义的router
s.AddRouter(0, &PingRouter{})
// 给当前zinx框架添加一个自定义的router
s.AddRouter(1, &HelloEmpty{})
// 启动Server
s.Run()
}
Go
1
https://gitee.com/yang-chengxian/empty_zinx.git
git@gitee.com:yang-chengxian/empty_zinx.git
yang-chengxian
empty_zinx
empty_zinx
v1.0.5

搜索帮助