-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
48 lines (41 loc) · 989 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
40
41
42
43
44
45
46
47
48
package server
import (
"fmt"
"sync"
as "github.com/godevsig/adaptiveservice"
)
type statMgr struct {
sync.RWMutex
clients map[string]struct{}
subscribers map[chan string]struct{}
sessionNum int32
counter int64
}
const (
// Publisher is the service(s) publisher
Publisher = "example"
// ServiceEcho is the echo service
ServiceEcho = "echo.v1.0"
)
// Run runs the server.
func Run(opts []as.Option) {
s := as.NewServer(opts...).SetPublisher(Publisher)
mgr := &statMgr{
clients: make(map[string]struct{}),
subscribers: make(map[chan string]struct{}),
}
if err := s.Publish(ServiceEcho,
echoKnownMsgs,
as.OnNewStreamFunc(mgr.onNewStream),
as.OnStreamCloseFunc(mgr.onStreamClose),
as.OnConnectFunc(mgr.onConnect),
as.OnDisconnectFunc(mgr.onDisconnect),
); err != nil {
fmt.Println(err)
return
}
if err := s.Serve(); err != nil { // ctrl+c to exit
fmt.Println(err)
}
fmt.Printf("echo server has served %d requests\n", mgr.counter)
}