forked from gautamrege/gochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
39 lines (31 loc) · 939 Bytes
/
server.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
package main
import (
"fmt"
"github.com/gautamrege/gochat/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
"sync"
)
type chatServer struct {
}
func (s *chatServer) Chat(ctx context.Context, req *api.ChatRequest) (res *api.ChatResponse, err error) {
fmt.Printf("\n%s\n> ", fmt.Sprintf("@%s says: \"%s\"", req.From.Name, req.Message))
// TODO-WORKSHOP-STEP-7: If this is a chat from an unknown user, insert into PeerHandleMap
return &api.ChatResponse{}, nil
}
// gRPC listener - register and start grpc server
func startServer(wg *sync.WaitGroup) {
defer wg.Done()
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", MyHandle.Host, MyHandle.Port))
if err != nil {
log.Fatalf("failed to startServer: %v", err)
}
grpcServer := grpc.NewServer()
api.RegisterGoChatServer(grpcServer, &chatServer{})
err = grpcServer.Serve(listener)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
}