You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/core/server_test.go

54 lines
1.1 KiB
Go

package core
import (
"fmt"
"git.noahlan.cn/noahlan/nnet/packet"
"git.noahlan.cn/noahlan/ntools-go/core/nlog"
"testing"
"time"
)
func TestServer(t *testing.T) {
server := NewServer(EngineConf{
Protocol: "tcp",
Addr: ":12345",
Name: "N-Net",
Mode: DevMode,
},
WithPacker(func() packet.Packer {
return packet.NewNNetPacker()
}),
WithSerializer(nil),
WithHeartbeatInterval(time.Hour),
WithProcessor(NewNNetProcessor()),
)
server.AddRoute(Route{
Matches: "test",
2 years ago
Handler: func(conn *connection, pkg packet.IPacket) {
fmt.Println(pkg)
p, ok := pkg.(*packet.Packet)
if !ok {
nlog.Error("wrong packet type")
return
}
bd := []byte("服务器接收到数据为: " + string(p.GetBody()))
// 注Response类型数据不需要Route原地返回客户端需等待
conn.Send(packet.Header{
PacketType: packet.Data,
Length: uint32(len(bd)),
MessageHeader: packet.MessageHeader{
MsgType: packet.Response,
ID: p.ID,
Route: p.Route,
},
}, bd)
},
})
defer server.Stop()
server.Start()
}