-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
93 lines (81 loc) · 2.3 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
zmq "github.com/pebbe/zmq4"
)
const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
maxMessageSize = 512
publisherSource = "tcp://localhost:5000"
)
var upgrader = websocket.Upgrader{}
type subscription struct {
subscribe string
}
type Client struct {
conn *websocket.Conn
}
func (client *Client) readPump() {
defer func() {
log.Printf("read pump closing")
client.conn.Close()
}()
client.conn.SetReadLimit(maxMessageSize)
client.conn.SetReadDeadline(time.Now().Add(pongWait))
client.conn.SetPongHandler(func(string) error {
client.conn.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
for {
_, message, error := client.conn.ReadMessage()
if error != nil {
if websocket.IsUnexpectedCloseError(error,
websocket.CloseGoingAway,
websocket.CloseAbnormalClosure) {
log.Printf("error: %v", error)
}
log.Printf("Breaking from reader")
break
}
log.Printf("received %v", message)
}
}
func (client *Client) writePump() {
subscriber, _ := zmq.NewSocket(zmq.SUB)
defer subscriber.Close()
subscriber.Connect(publisherSource)
subscriber.SetSubscribe("")
for {
message, error := subscriber.RecvMessage(0)
if error != nil {
log.Fatalf("Trouble reading from zeromq %v", error)
}
log.Printf("Received %v", message)
data := []byte(message[1])
error = client.conn.WriteMessage(websocket.TextMessage, data)
if error != nil {
log.Printf("%v\n", error)
break
}
}
}
func serve(writer http.ResponseWriter, request *http.Request) {
_client, error := upgrader.Upgrade(writer, request, nil)
if error != nil {
log.Print("upgrade: ", error)
return
}
client := Client{_client}
go client.readPump()
go client.writePump()
}
func main() {
http.HandleFunc("/ws", serve)
http.Handle("/", http.FileServer(http.Dir("./static")))
log.Fatal(http.ListenAndServe(":8000", nil))
}