-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
60 lines (54 loc) · 1.3 KB
/
connection.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
// Copyright 2020 The Verbis Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package livereload
import (
"bytes"
"github.com/gorilla/websocket"
"sync"
)
// Connection defines a singular connection to the ws.
// Including the []byte chan for sending websocket.Message's
// to the client.
type connection struct {
// The websocket connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
// Potential data race.
closer sync.Once
}
// close closes the connection once.
func (c *connection) close() {
c.closer.Do(func() {
close(c.send)
})
}
// reader reads incoming messages and sends the hello
// command to the client.
func (c *connection) reader() {
for {
_, message, err := c.ws.ReadMessage()
if err != nil {
break
}
if bytes.Contains(message, []byte(`"command":"hello"`)) {
c.send <- []byte(`{
"command": "hello",
"protocols": [ "http://livereload.com/protocols/official-7" ],
"serverName": "Verbis"
}`)
}
}
c.ws.Close()
}
// writer writes the websocket.TextMessage to the websocket.
func (c *connection) writer() {
for message := range c.send {
err := c.ws.WriteMessage(websocket.TextMessage, message)
if err != nil {
break
}
}
c.ws.Close()
}