Xin is a Golang-based TCP server framework
go get -u github.com/xuwuruoshui/xin
server
func main(){
// 1、create xin server
s := xnet.NewServer()
// 2、register Connection Hook
s.SetOnConnStart(afterConnection)
s.SetOnConnStop(beforeStop)
// 3、Add Router(write your logic to router's handle)
s.AddRouter(1, &PingRouter{})
// 4、run server
s.Run()
}
// imple BaseRouter
type HelloRouter struct {
xnet.BaseRouter
}
// write your logic
func (h *HelloRouter) Handle(req xifs.XRequest) {
log.Println("Call Router Handle")
// Read Client Data
log.Printf("Recv from clien:msgId=%d,data=%s\n", req.MsgId(), req.Data())
// Write Client Data
if err := req.Connection().SendMsg(req.MsgId(), []byte("XinV1.0 server Hello...")); err != nil {
log.Println("send msg error:", err)
}
}
client
func main() {
// conn to server
conn, err := net.Dial("tcp", ":7777")
if err != nil {
log.Println("Client start error:", err)
return
}
for {
datapack := xnet.NewDataPack()
// 1.send msg to server
msg := xnet.NewMessage(1, []byte("Xin V1.0 client Test message!!!"))
packData, err := datapack.Pack(msg)
if err != nil {
log.Println("pack data error:", err)
break
}
_, err = conn.Write(packData)
if err != nil {
log.Println("write data error:", err)
break
}
//2. recv msg
data := make([]byte, datapack.GetHeadLen())
if _, err := conn.Read(data); err != nil {
break
}
msgRecv, err := datapack.Unpack(data)
if err != nil {
break
}
if msgRecv.GetLength() > 0 {
msgRecv.SetData(make([]byte, msgRecv.GetLength()))
_, err := conn.Read(msgRecv.GetData())
if err != nil {
break
}
log.Println(msgRecv.GetId(), msgRecv.GetLength(), string(msgRecv.GetData()))
}
time.Sleep(time.Second)
}
}
example
open server,client,client2
use go run xxx.go