-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
58 lines (47 loc) · 1.08 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"hacknc2022/protobuf"
"io"
"log"
"math/rand"
"time"
"google.golang.org/grpc"
)
func runStreamTicker(client protobuf.TradeServiceClient) error {
log.Printf("Streaming signals")
ctx := context.Background()
stream, err := client.StreamTicker(ctx)
if err != nil {
log.Fatalf("%v.Execute(ctx) = %v, %v: ", client, stream, err)
}
for {
ticker, err := stream.Recv()
if err == io.EOF {
log.Println("EOF")
return nil
}
if err != nil {
log.Printf("Err: %v", err)
}
log.Printf("Client recieved ticker: %f", ticker.GetValue())
// BUY / SELL Logic Here
rand.Seed(time.Now().UnixNano())
n := 0 + rand.Intn(2-0+1)
if err := stream.Send(&protobuf.Signal{Value: int32(n)}); err != nil {
return err
}
}
}
func main() {
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
opts = append(opts, grpc.WithBlock())
conn, err := grpc.Dial("localhost:9111", opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := protobuf.NewTradeServiceClient(conn)
runStreamTicker(client)
}