89 lines
1.7 KiB
Go
Raw Normal View History

2015-12-02 22:44:01 +00:00
package main
import (
"fmt"
"github.com/micro/go-micro/cmd"
example "github.com/micro/go-micro/examples/server/proto/example"
"golang.org/x/net/context"
)
var (
cl = example.NewExampleClient("go.micro.srv.example", nil)
2015-12-02 22:44:01 +00:00
)
func call(i int) {
rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"})
if err != nil {
fmt.Println("call err: ", err, rsp)
return
}
fmt.Println("Call:", i, "rsp:", rsp.Msg)
}
2015-12-18 21:51:51 +00:00
func stream(i int) {
stream, err := cl.Stream(context.Background(), &example.StreamingRequest{Count: int64(i)})
2015-12-02 22:44:01 +00:00
if err != nil {
fmt.Println("err:", err)
return
}
2015-12-18 21:51:51 +00:00
for j := 0; j < i; j++ {
rsp, err := stream.RecvR()
2015-12-02 22:44:01 +00:00
if err != nil {
fmt.Println("err:", err)
break
}
fmt.Println("Stream: rsp:", rsp.Count)
}
if stream.Error() != nil {
fmt.Println("stream err:", err)
return
}
if err := stream.Close(); err != nil {
fmt.Println("stream close err:", err)
}
}
2015-12-18 21:51:51 +00:00
func pingPong(i int) {
stream, err := cl.PingPong(context.Background())
if err != nil {
fmt.Println("err:", err)
return
}
for j := 0; j < i; j++ {
if err := stream.SendR(&example.Ping{Stroke: int64(j)}); err != nil {
fmt.Println("err:", err)
return
}
rsp, err := stream.RecvR()
if err != nil {
fmt.Println("recv err", err)
break
}
fmt.Printf("Sent ping %v got pong %v\n", j, rsp.Stroke)
}
if stream.Error() != nil {
fmt.Println("stream err:", err)
return
}
if err := stream.Close(); err != nil {
fmt.Println("stream close err:", err)
}
}
2015-12-02 22:44:01 +00:00
func main() {
cmd.Init()
fmt.Println("\n--- Call example ---\n")
for i := 0; i < 10; i++ {
call(i)
}
2015-12-18 21:51:51 +00:00
2015-12-02 22:44:01 +00:00
fmt.Println("\n--- Streamer example ---\n")
2015-12-18 21:51:51 +00:00
stream(10)
fmt.Println("\n--- Ping Pong example ---\n")
pingPong(10)
2015-12-02 22:44:01 +00:00
}