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.
|
|
|
|
package core
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"git.noahlan.cn/northlan/nnet/log"
|
|
|
|
|
"git.noahlan.cn/northlan/nnet/packet"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestServer(t *testing.T) {
|
|
|
|
|
server := NewServer(EngineConf{
|
|
|
|
|
Protocol: "tcp",
|
|
|
|
|
Addr: ":12345",
|
|
|
|
|
Name: "N-Net",
|
|
|
|
|
Mode: DevMode,
|
|
|
|
|
},
|
|
|
|
|
WithPacker(packet.NewNNetPacker()),
|
|
|
|
|
WithSerializer(nil),
|
|
|
|
|
WithHeartbeatInterval(time.Hour),
|
|
|
|
|
WithProcessor(NewNNetProcessor()),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
server.AddRoute(Route{
|
|
|
|
|
Matches: "test",
|
|
|
|
|
Handler: func(conn *Connection, pkg packet.IPacket) {
|
|
|
|
|
fmt.Println(pkg)
|
|
|
|
|
p, ok := pkg.(*packet.Packet)
|
|
|
|
|
if !ok {
|
|
|
|
|
log.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()
|
|
|
|
|
}
|