3 Star 5 Fork 4

Gitee 极速下载 / Tao

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/leesper/tao
克隆/下载
server.go 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"context"
"net"
"runtime"
"github.com/leesper/holmes"
"github.com/leesper/tao"
"github.com/leesper/tao/examples/pingpong"
)
// PingPongServer defines pingpong server.
type PingPongServer struct {
*tao.Server
}
// NewPingPongServer returns PingPongServer.
func NewPingPongServer() *PingPongServer {
onConnect := tao.OnConnectOption(func(conn tao.WriteCloser) bool {
holmes.Infoln("on connect")
return true
})
onError := tao.OnErrorOption(func(conn tao.WriteCloser) {
holmes.Infoln("on error")
})
onClose := tao.OnCloseOption(func(conn tao.WriteCloser) {
holmes.Infoln("closing pingpong client")
})
return &PingPongServer{
tao.NewServer(onConnect, onError, onClose),
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
defer holmes.Start().Stop()
tao.MonitorOn(12345)
tao.Register(pingpong.PingPontMessage, pingpong.DeserializeMessage, ProcessPingPongMessage)
l, err := net.Listen("tcp", ":12346")
if err != nil {
holmes.Fatalln("listen error", err)
}
server := NewPingPongServer()
server.Start(l)
}
// ProcessPingPongMessage handles business logic.
func ProcessPingPongMessage(ctx context.Context, conn tao.WriteCloser) {
ping := tao.MessageFromContext(ctx).(pingpong.Message)
holmes.Infoln(ping.Info)
rsp := pingpong.Message{
Info: "pong",
}
conn.Write(rsp)
}
Go
1
https://gitee.com/mirrors/Tao.git
git@gitee.com:mirrors/Tao.git
mirrors
Tao
Tao
2c485cb2042e

搜索帮助