-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
76 lines (68 loc) · 1.96 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Example usage code.
package main
import (
"log"
"time"
client "github.com/ausrasul/backbone/client"
server "github.com/ausrasul/backbone/server"
)
func main() {
go start_server()
c := client.New("client1", ":1234")
c.SetOnConnect(func(c *client.Client) {
log.Println("client doing stuff when connected")
c.SetCommandHandler("response", func(c *client.Client, arg string) {
time.Sleep(time.Millisecond * 500)
c.Send("test_command", "more stuff")
log.Println("received response , ", arg)
})
})
c.SetOnDisconnect(func(*client.Client) { log.Println("client disconnected!") })
log.Println("client connecting...")
c.Start()
log.Println("client connected...")
c.Send("test_command", "client is sending this command")
<-time.After(time.Second * 5)
}
func start_server() {
log.Println("starting server")
s := server.New("localhost:1234")
inChan := make(chan string, 5)
s.SetOnConnect(func(str string) {
log.Println("server: a client connected")
inChan <- "something connected " + str
})
s.SetOnDisconnect(func(str string) {
log.Println("server: client disconnected")
inChan <- "something disconnect " + str
})
handler := func(client_id string, arg string) {
log.Println("server handling cmd")
inChan <- "command: " + client_id + " -- " + arg
s.Send(client_id, "response", "cmdArg")
log.Println("server handled cmd")
}
s.SetCommandHandler("client1", "test_command", handler)
go s.Start()
log.Println("started.")
for in := range inChan {
log.Println("svr got ", in)
}
log.Println("end.")
}
/* this is how client should work
go start_server()
conn, err := grpc.Dial(":1234", grpc.WithInsecure())
client := comm.NewCommClient(conn)
stream, err := client.OpenComm(context.Background())
stream.Send(&comm.Command{Name: "id", Arg: "client1"}) // we have to send this as first command.
for {
in, err := stream.Recv()
if err == io.EOF {
// read done.
close(waitc)
return
}
stream.Send(&comm.Command{Name: "test_command", Arg: "test args"})
}
*/