Пример #1
0
func responder(port string) {
	// A simple echo operation with a 500ms response delay
	gotalk.HandleBufferRequest("echo", func(s *gotalk.Sock, op string, buf []byte) ([]byte, error) {
		println("responder: handling request")
		return buf, nil
	})

	// Start a server
	s, err := gotalk.Listen("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}

	// Print when we receive heartbeats
	s.OnHeartbeat = func(load int, t time.Time) {
		fmt.Printf("responder: received heartbeat: load=%v, time=%v\n", load, t)
	}

	// Configure limits with a read timeout of one second
	s.Limits = gotalk.NewLimits(0, 0)
	s.Limits.SetReadTimeout(time.Second)

	// Accept connections
	println("responder: listening at", s.Addr())
	go s.Accept()
}
Пример #2
0
func client(port string) {
	gotalk.HandleBufferRequest("ping", func(_ *gotalk.Sock, _ string, b []byte) ([]byte, error) {
		fmt.Printf("client: handling 'ping' request: %q\n", string(b))
		return []byte("pong"), nil
	})

	// Connect to the server
	s, err := gotalk.Connect("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	fmt.Printf("client: connected to %s\n", s.Addr())

	// Send a notification as JSON
	fmt.Printf("client: sending 'msg' notification\n")
	if err := s.Notify("msg", struct{ Msg string }{"World"}); err != nil {
		fmt.Printf("client: notification: %v\n", err.Error())
	}

	// Send a notification as byte string
	fmt.Printf("client: sending 'msg' notification\n")
	if err := s.BufferNotify("msg", []byte("Hello")); err != nil {
		fmt.Printf("client: notification: error %v\n", err.Error())
	}

	// Send a request & read result via JSON-encoded go values
	fmt.Printf("client: sending 'greet' request\n")
	greeting := GreetOut{}
	if err := s.Request("greet", GreetIn{"Rasmus"}, &greeting); err != nil {
		fmt.Printf("client: greet: error %v\n", err.Error())
	} else {
		fmt.Printf("client: greet: %+v\n", greeting)
	}

	// Send a request & read result as byte strings
	fmt.Printf("client: sending 'echo' request\n")
	b, err := s.BufferRequest("echo", []byte("abc"))
	if err != nil {
		fmt.Printf("client: echo: error %v\n", err.Error())
	} else {
		fmt.Printf("client: echo: %v\n", string(b))
	}

	s.Close()
}
Пример #3
0
func main() {
	gotalk.Handle("greet", func(in GreetIn) (GreetOut, error) {
		println("in greet handler: in.Name=", in.Name)
		return GreetOut{"Hello " + in.Name}, nil
	})

	gotalk.HandleBufferRequest("echó", func(s *gotalk.Sock, op string, b []byte) ([]byte, error) {
		return b, nil
	})

	ws := gotalk.WebSocketHandler()
	ws.OnAccept = onAccept
	http.Handle("/gotalk/", ws)
	http.Handle("/", http.FileServer(http.Dir(".")))
	err := http.ListenAndServe(":1234", nil)
	if err != nil {
		panic(err)
	}
}
Пример #4
0
func responder(port string) {
	// A simple echo operation with a 500ms response delay
	gotalk.HandleBufferRequest("echo", func(s *gotalk.Sock, op string, buf []byte) ([]byte, error) {
		println("responder: handling request")
		time.Sleep(time.Millisecond * 400)
		return buf, nil
	})

	// Start a server
	s, err := gotalk.Listen("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}

	// Limit this server to 5 concurrent requests (and disable streaming messages)
	s.Limits = gotalk.NewLimits(5, 0)

	// Accept connections
	println("listening at", s.Addr())
	go s.Accept()
}