-
Notifications
You must be signed in to change notification settings - Fork 75
/
client.go
53 lines (43 loc) · 930 Bytes
/
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
package main
import (
"log"
"net"
"time"
"github.com/lesismal/arpc"
)
// OnBroadcast .
func OnBroadcast(ctx *arpc.Context) {
ret := ""
ctx.Bind(&ret)
log.Printf("OnBroadcast: \"%v\"", ret)
}
func dialer() (net.Conn, error) {
return net.DialTimeout("tcp", "localhost:8888", time.Second*3)
}
func main() {
var clients []*arpc.Client
arpc.DefaultHandler.Handle("/broadcast", OnBroadcast)
for i := 0; i < 10; i++ {
client, err := arpc.NewClient(dialer)
if err != nil {
log.Println("NewClient failed:", err)
return
}
defer client.Stop()
clients = append(clients, client)
}
for i := 0; i < 10; i++ {
client := clients[i]
go func() {
passwd := "123qwe"
response := ""
err := client.Call("/enter", passwd, &response, time.Second*5)
if err != nil {
log.Printf("Call failed: %v", err)
} else {
log.Printf("Call Response: \"%v\"", response)
}
}()
}
<-make(chan int)
}