1 Star 0 Fork 0

小鱼儿小董子/dongli-zinx

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
server.go 1.73 KB
Copy Edit Raw Blame History
小鱼儿小董子 authored 2025-01-14 22:07 +08:00 . first commit
package main
import (
"errors"
"fmt"
"time"
"gitee.com/wanjimao/dongli-zinx/ziface"
"gitee.com/wanjimao/dongli-zinx/znet"
)
type TestRouter struct {
znet.BaseRouter
}
// PreHandle -
func (t *TestRouter) PreHandle(req ziface.IRequest) {
start := time.Now()
fmt.Println("--> Call PreHandle")
if err := req.GetConnection().SendMsg(0, []byte("test1")); err != nil {
fmt.Println(err)
}
elapsed := time.Since(start)
fmt.Println("cost time:", elapsed)
}
// Handle -
func (t *TestRouter) Handle(req ziface.IRequest) {
fmt.Println("--> Call Handle")
// Simulated scenario - In the event of an expected error such as incorrect permissions or incorrect information,
// subsequent function execution will be stopped, but this function will be fully executed.
// 模拟场景- 出现意料之中的错误 如权限不对或者信息错误 则停止后续函数执行,但是次函数会执行完毕
if err := Err(); err != nil {
req.Abort()
fmt.Println("Insufficient permission")
}
// Simulation scenario - In case of a certain situation, repeat the above operation.
// 模拟场景- 出现某种情况,重复上面的操作
/*
if err := Err(); err != nil {
req.Goto(znet.PRE_HANDLE)
fmt.Println("repeat")
}
*/
if err := req.GetConnection().SendMsg(0, []byte("test2")); err != nil {
fmt.Println(err)
}
time.Sleep(1 * time.Millisecond)
}
// PostHandle -
func (t *TestRouter) PostHandle(req ziface.IRequest) {
fmt.Println("--> Call PostHandle")
if err := req.GetConnection().SendMsg(0, []byte("test3")); err != nil {
fmt.Println(err)
}
}
func Err() error {
//Specific Business Operation (具体业务操作)
return errors.New("Test")
}
func main() {
s := znet.NewServer()
s.AddRouter(1, &TestRouter{})
s.Serve()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wanjimao/dongli-zinx.git
git@gitee.com:wanjimao/dongli-zinx.git
wanjimao
dongli-zinx
dongli-zinx
v0.0.1

Search