-
Notifications
You must be signed in to change notification settings - Fork 0
/
seismic.go
125 lines (111 loc) · 2.62 KB
/
seismic.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package goseismic
import (
"log"
"net/url"
"time"
"github.com/gorilla/websocket"
)
const seismicURLHost = "www.seismicportal.eu"
const seismicURLPath = "/standing_order/websocket"
const reconnectWait = 15 * time.Second
const pingWait = 60 * time.Second
// Seismic struct is type used for receiving events from websocket
type Seismic struct {
conn *websocket.Conn
connected bool
KeepAlive bool
Debug bool
Events chan Event
}
// NewSeismic creates new Seismic value which contains Event channel for receiving seismic events
func NewSeismic() *Seismic {
s := &Seismic{
KeepAlive: true,
Debug: false,
Events: make(chan Event),
}
go s.sendPings()
return s
}
// Connect connects to Seismic portal websocket
func (s *Seismic) Connect() error {
u := url.URL{Scheme: "wss", Host: seismicURLHost, Path: seismicURLPath}
var err error
for {
s.log("Connecting to websocket...")
s.conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
if err == nil {
s.conn.SetPongHandler(s.pongHandler)
s.conn.SetCloseHandler(s.closeHandler)
s.connected = true
go s.readMessages()
s.log("Connected to websocket.")
return nil
}
if !s.KeepAlive {
break
}
time.Sleep(reconnectWait)
}
s.log("Error while connecting to websocket:", err)
return err
}
// ReadMessages reads new events (JSON) from seismic portal websocket, parse it and sends to channel
func (s *Seismic) readMessages() {
for {
if !s.connected {
break
}
_, message, err := s.conn.ReadMessage()
if err == nil {
if event, err := ParseEvent(message); err == nil {
s.Events <- event
} else {
s.log("Error while parsing JSON")
}
} else {
s.log("Error while reading message from websocket:", err)
s.Disconnect()
break
}
}
}
// Disconnect disconnects from Seismic portal websocket
func (s *Seismic) Disconnect() error {
if s.connected {
s.connected = false
s.log("Disconnecting from websocket...")
return s.conn.Close()
}
return nil
}
// sendPings sends control messages (ping) every pingWait interval to keep connection alive
func (s *Seismic) sendPings() {
ticker := time.NewTicker(pingWait)
defer ticker.Stop()
for {
<-ticker.C
if s.KeepAlive {
if s.connected {
s.log("Sending ping...")
s.conn.WriteMessage(websocket.PingMessage, []byte{})
} else {
s.Connect()
}
}
}
}
func (s *Seismic) pongHandler(string) error {
s.log("Received pong.")
return nil
}
func (s *Seismic) closeHandler(code int, text string) error {
s.log("Close handler called.")
s.connected = false
return nil
}
func (s *Seismic) log(messages ...interface{}) {
if s.Debug {
log.Println(messages...)
}
}