Beispiel #1
0
// This is an echo client demo work with the echo_server.
// usage:
//     go run github.com/funny/examples/echo_client/main.go
func main() {
	protocol := link.NewFixProtocol(4, binary.BigEndian)

	client, err := link.Dial("tcp", "127.0.0.1:10010", protocol)
	if err != nil {
		panic(err)
	}

	client.OnMessage(func(session *link.Session, message []byte) {
		println("message:", string(message))
	})

	client.OnClose(func(session *link.Session) {
		println("closed")
	})

	client.Start()

	for {
		var input string
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			break
		}
		client.Send(EchoMessage{input})
	}

	client.Close()

	println("bye")
}
Beispiel #2
0
// This is an echo server demo work with the echo_client.
// usage:
//     go run github.com/funny/examples/echo_server/main.go
func main() {
	protocol := link.NewFixProtocol(4, binary.BigEndian)

	server, err := link.ListenAndServe("tcp", "127.0.0.1:10010", protocol)
	if err != nil {
		panic(err)
	}

	println("server start")

	server.Handle(func(session *link.Session) {
		println("client", session.RawConn().RemoteAddr().String(), "in")

		session.OnMessage(func(session *link.Session, message []byte) {
			println("client", session.RawConn().RemoteAddr().String(), "say:", string(message))

			session.Send(EchoMessage{message})
		})

		session.OnClose(func(session *link.Session) {
			println("client", session.RawConn().RemoteAddr().String(), "close")
		})
	})
}