forked from gautamrege/gochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
44 lines (37 loc) · 1.01 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
package main
import (
"context"
"fmt"
"github.com/gautamrege/gochat/api"
"google.golang.org/grpc"
"log"
"time"
)
func sendChat(receiverHandle api.Handle, message string) {
receiverConnStr := fmt.Sprintf("%s:%d", receiverHandle.Host, receiverHandle.Port)
receiverConn, err := grpc.Dial(receiverConnStr, grpc.WithInsecure())
defer receiverConn.Close()
if err != nil {
log.Fatalf("fail to dial: %v", err)
return
}
chatClient := api.NewGoChatClient(receiverConn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
/****
// THIS CODE IS FOR REFERENCE ONLY FROM THE pb PACKAGE. DO NOT UNCOMMENT
type api.ChatRequest struct {
From *api.Handle
To *api.Handle
Message string
}
*****/
var req api.ChatRequest
// TODO-WORKSHOP-STEP-8: Create req struct of type api.ChatRequest to send to client.Chat method
_, err = chatClient.Chat(ctx, &req)
if err != nil {
log.Printf("ERROR: Chat(): %v", err)
USERS.Delete(receiverHandle.Name)
}
return
}