Exemplo n.º 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()
}
Exemplo n.º 2
0
func responder(port string) {
	// Handle streaming request & result
	gotalk.HandleStreamRequest("joke", handleJoke)

	// Accept connections
	s, err := gotalk.Listen("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	s.Limits = gotalk.NoLimits // Remove any limits on streaming requests
	fmt.Printf("responder: listening at %s\n", s.Addr())
	go s.Accept()
}
Exemplo n.º 3
0
func server(port string) {
	// Use a separate set of handlers for the server, as we are running the client in the same
	// program and thus we would have both server and client register handlers on DefaultHandlers.
	handlers := gotalk.NewHandlers()

	// Handle JSON-encoded request & result
	handlers.Handle("greet", func(in GreetIn) (GreetOut, error) {
		fmt.Printf("server: handling 'greet' request: %+v\n", in)
		return GreetOut{"Hello " + in.Name}, nil
	})

	// Handle buffered request & result
	handlers.HandleBufferRequest("echo", func(_ *gotalk.Sock, _ string, b []byte) ([]byte, error) {
		fmt.Printf("server: handling 'echo' request: %q\n", string(b))
		return b, nil
	})

	// Handle all notifications
	handlers.HandleBufferNotification("", func(s *gotalk.Sock, name string, b []byte) {
		fmt.Printf("server: received notification: %q => %q\n", name, string(b))

		// Send a request to the other end.
		// Note that we must do this in a goroutine as we would otherwise block this function from
		// returning the response, meaning our client() function would block indefinitely on waiting
		// for a response.
		go func() {
			fmt.Printf("server: sending 'ping' request\n")
			reply, err := s.BufferRequest("ping", []byte("abc"))
			if err != nil {
				fmt.Printf("server: ping: error %v\n", err.Error())
			} else {
				fmt.Printf("server: ping: %v\n", string(reply))
			}
		}()
	})

	// Accept connections
	s, err := gotalk.Listen("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	s.Handlers = handlers
	fmt.Printf("server: listening at %s\n", s.Addr())
	go s.Accept()
}
Exemplo n.º 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()
}