func main() { tcpAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:11125") checkError(err) conn, err := net.DialTCP("tcp", nil, tcpAddr) checkError(err) fmt.Println("connected server....") ReadStdinChan = make(chan string) ReadServerChan = make(chan string) go ReadFromStdin() go ReadFromServer(conn) for { select { case p := <-ReadStdinChan: fmt.Println("read from stdin:", p) // conn.Write(protocol.NewEchoPacket([]byte(p), false).Serialize()) case v := <-ReadServerChan: fmt.Printf("Server reply:\n", v) default: } } conn.Close() }
func (this *Callback) OnMessage(c *gotcpServer.Conn, p gotcpServer.Packet) bool { echoPacket := p.(*protocol.EchoPacket) fmt.Printf("OnMessage:%v\n", string(echoPacket.GetBody())) c.WritePkt(protocol.NewEchoPacket(echoPacket.Serialize(), true), time.Second) return true }