-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.go
78 lines (68 loc) · 1.26 KB
/
websocket.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
package bigbro
import (
"github.com/gorilla/websocket"
"log"
"net/http"
"time"
)
// Upgrader upgrades a web socket.
var Upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// WsEvent handles reading events from the web socket.
func WsEvent(ws *websocket.Conn, l Logger) {
readWait := 1 * time.Millisecond
readTicker := time.NewTicker(readWait)
// defer closing of web socket
defer func() {
readTicker.Stop()
_ = ws.Close()
}()
for {
select {
case <-readTicker.C:
var event Event
err := ws.ReadJSON(&event)
if err != nil {
log.Println(err)
return
}
err = l.Log(event)
if err != nil {
log.Println(err)
return
}
}
}
}
// WsRecord handles reading screen captures from the web socket.
func WsRecord(ws *websocket.Conn) {
readWait := 1 * time.Millisecond
readTicker := time.NewTicker(readWait)
// defer closing of web socket
defer func() {
readTicker.Stop()
_ = ws.Close()
}()
for {
select {
case <-readTicker.C:
var capture Capture
err := ws.ReadJSON(&capture)
if err != nil {
log.Println(err)
return
}
go func() {
err = WriteCapture(capture)
if err != nil {
log.Println(err)
return
}
}()
}
}
}