rts-sim-testing-service/third_party/udp/udp_server_test.go

39 lines
678 B
Go

package udp_test
import (
"log/slog"
"testing"
"joylink.club/bj-rtsts-server/third_party/udp"
)
type TestMsg struct {
Msg string
Port int
}
func (t *TestMsg) Encode() []byte {
b := []byte(t.Msg)
// binary.BigEndian.PutUint16(b, uint16(t.Port))
return b
}
func (t *TestMsg) Decode(data []byte) error {
t.Msg = string(data)
// t.Port = int(binary.BigEndian.Uint16(data))
return nil
}
func handleUdpMsg(b []byte) {
slog.Info("udp server handle", "msg", string(b))
}
func TestUdpServer(t *testing.T) {
udp.NewServer("127.0.0.1:7777", handleUdpMsg).Listen()
client := udp.NewClient("127.0.0.1:7777")
client.SendMsg(&TestMsg{
Msg: "test, port = 7777",
})
}