/
server.go
61 lines (53 loc) · 1.24 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
package main
import (
"./chatroom"
"fmt"
)
import zmq "github.com/alecthomas/gozmq"
func admin_server(c chan int) {
// TODO maybe create another zmq channel
var seq int64 = 0
for {
num := <-c
if num == 1 {
seq += int64(num)
} else {
fmt.Println("[DEBUG] stat: total message:", seq)
}
}
}
func bind_to_channel(sock zmq.Socket) (channel chan []byte) {
channel = make(chan []byte)
go func(){
for {
msg, err := sock.Recv(0)
if err != nil {
fmt.Println("[ERROR] die at sock.Recv:", err.Error())
break
}
channel <- msg
}
}()
return
}
func main() {
context, _ := zmq.NewContext()
publisher, _ := context.NewSocket(zmq.PUB)
defer publisher.Close()
publisher.Bind(chatroom.PUBLISHER_ADDRESS)
router, _ := context.NewSocket(zmq.PULL)
defer router.Close()
router.Bind(chatroom.POST_ADDRESS)
stat_channel := make(chan int)
go admin_server(stat_channel)
receiver_channel := bind_to_channel(router)
for {
select {
case msg := <-receiver_channel:
go func() {
stat_channel <- 1
publisher.Send(msg, 0)
}()
}
}
}