-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
42 lines (30 loc) · 1000 Bytes
/
util.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
package pubsub
import (
"time"
"github.com/gorilla/websocket"
"github.com/MicahParks/websocket-pubsub/clients"
)
const (
// pingRatio is the ratio of when pings are sent to when pongs are expected.
pingRatio = .7 // TODO Could make constant configurable.
)
// sendPing sends a ping to the client's websocket.
func sendPing(conn *websocket.Conn, pongDeadline time.Duration) (err error) {
// Expect the upcoming ping to be written within a short duration.
if err = clients.ShortWriteDeadline(conn, pongDeadline); err != nil {
return err
}
// Send the ping.
if innerErr := conn.WriteMessage(websocket.PingMessage, nil); innerErr != nil {
return err
}
return nil
}
// setReadDeadline sets a read deadline equal to the PongDeadline.
func setReadDeadline(conn *websocket.Conn, pongDeadline time.Duration) (err error) {
// Set the read deadline equal to the pong deadline.
if err = conn.SetReadDeadline(time.Now().Add(pongDeadline)); err != nil {
return err
}
return nil
}