-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_rest_ws.go
117 lines (99 loc) · 2.79 KB
/
api_rest_ws.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
package api_rest
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/bettercap/bettercap/session"
"github.com/gorilla/websocket"
)
const (
// Time allowed to write an event to the client.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the client.
pongWait = 60 * time.Second
// Send pings to client with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
)
func (mod *RestAPI) streamEvent(ws *websocket.Conn, event session.Event) error {
msg, err := json.Marshal(event)
if err != nil {
mod.Error("Error while creating websocket message: %s", err)
return err
}
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.TextMessage, msg); err != nil {
if !strings.Contains(err.Error(), "closed connection") {
mod.Error("Error while writing websocket message: %s", err)
return err
}
}
return nil
}
func (mod *RestAPI) sendPing(ws *websocket.Conn) error {
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
mod.Error("Error while writing websocket ping message: %s", err)
return err
}
return nil
}
func (mod *RestAPI) streamWriter(ws *websocket.Conn, w http.ResponseWriter, r *http.Request) {
defer ws.Close()
// first we stream what we already have
events := session.I.Events.Sorted()
n := len(events)
if n > 0 {
mod.Debug("Sending %d events.", n)
for _, event := range events {
if err := mod.streamEvent(ws, event); err != nil {
return
}
}
}
session.I.Events.Clear()
mod.Debug("Listening for events and streaming to ws endpoint ...")
pingTicker := time.NewTicker(pingPeriod)
listener := session.I.Events.Listen()
defer session.I.Events.Unlisten(listener)
for {
select {
case <-pingTicker.C:
if err := mod.sendPing(ws); err != nil {
return
}
case event := <-listener:
if err := mod.streamEvent(ws, event); err != nil {
return
}
case <-mod.quit:
mod.Info("Stopping websocket events streamer ...")
return
}
}
}
func (mod *RestAPI) streamReader(ws *websocket.Conn) {
defer ws.Close()
ws.SetReadLimit(512)
ws.SetReadDeadline(time.Now().Add(pongWait))
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, _, err := ws.ReadMessage()
if err != nil {
mod.Debug("Closing websocket reader.")
break
}
}
}
func (mod *RestAPI) startStreamingEvents(w http.ResponseWriter, r *http.Request) {
ws, err := mod.upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
mod.Error("Error while updating api.rest connection to websocket: %s", err)
}
return
}
mod.Debug("Websocket streaming started for %s", r.RemoteAddr)
go mod.streamWriter(ws, w, r)
mod.streamReader(ws)
}