test.go 513 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "net"
  6. )
  7. func main() {
  8. conn, err := net.Dial("tcp", "127.0.0.1:3563")
  9. fmt.Print("test:")
  10. if err != nil {
  11. fmt.Print(err)
  12. panic(err)
  13. }
  14. // Hello 消息(JSON 格式)
  15. // 对应游戏服务器 Hello 消息结构体
  16. data := []byte(`{
  17. "Hello": {
  18. "Name": "leaf"
  19. }
  20. }`)
  21. // len + data
  22. m := make([]byte, 2+len(data))
  23. // 默认使用大端序
  24. binary.BigEndian.PutUint16(m, uint16(len(data)))
  25. copy(m[2:], data)
  26. // 发送消息
  27. conn.Write(m)
  28. }