-
Notifications
You must be signed in to change notification settings - Fork 75
/
server.go
69 lines (59 loc) · 1.4 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
package main
import (
"fmt"
"log"
"sync"
"time"
"github.com/lesismal/arpc"
)
var mux = sync.RWMutex{}
var server = arpc.NewServer()
var clientMap = make(map[*arpc.Client]struct{})
func main() {
server.Handler.Handle("/enter", func(ctx *arpc.Context) {
passwd := ""
ctx.Bind(&passwd)
if passwd == "123qwe" {
// keep client
mux.Lock()
clientMap[ctx.Client] = struct{}{}
mux.Unlock()
ctx.Write(nil)
log.Printf("enter success")
} else {
log.Printf("enter failed invalid passwd: %v", passwd)
ctx.Client.Stop()
}
})
// release client
server.Handler.HandleDisconnected(func(c *arpc.Client) {
mux.Lock()
delete(clientMap, c)
mux.Unlock()
})
go func() {
ticker := time.NewTicker(time.Second)
for i := 0; true; i++ {
<-ticker.C
switch i % 4 {
case 0:
server.Broadcast("/broadcast", fmt.Sprintf("Broadcast msg %d", i))
case 1:
server.BroadcastWithFilter("/broadcast", fmt.Sprintf("BroadcastWithFilter msg %d", i), func(c *arpc.Client) bool {
return true
})
case 2:
server.ForEach(func(c *arpc.Client) {
c.Notify("/broadcast", fmt.Sprintf("ForEach msg %d", i), arpc.TimeZero)
})
case 3:
server.ForEachWithFilter(func(c *arpc.Client) {
c.Notify("/broadcast", fmt.Sprintf("ForEachWithFilter msg %d", i), arpc.TimeZero)
}, func(c *arpc.Client) bool {
return true
})
}
}
}()
server.Run("localhost:8888")
}