/
socket.go
106 lines (90 loc) · 2.37 KB
/
socket.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
package belt
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/websocket"
"github.com/jinzhu/gorm"
)
func (app *App) SetBelt(w http.ResponseWriter, r *http.Request) {
optionID, err := strconv.Atoi(r.PostFormValue("optionID"))
if err != nil {
log.Println("[GetAddr] bad option id", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if optionID == -1 {
app.hub.SetHolder(int64(optionID))
app.hub.sendBeltUnset()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"ok": true}`)
return
}
db, ok := r.Context().Value("database").(*gorm.DB)
if !ok {
log.Println("could not find database in context")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
var opt Option
if res := db.First(&opt, uint(optionID)); res.Error != nil {
log.Printf("[GetAddr] '%d' %s\n", optionID, res.Error)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// TODO: validate back reference
app.hub.SetHolder(int64(opt.ID))
if err := app.hub.sendBeltUpdate(opt); err != nil {
log.Println("[SetBelt][app.sendBeltUpdate]", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"ok": true}`)
}
var userID int64
func (app *App) Socket(w http.ResponseWriter, r *http.Request) {
c, err := app.Upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("[app.Socket] upgrade:", err)
return
}
defer c.Close()
ch := make(chan []byte, 10)
id, err := app.hub.Subscribe(ch)
if err != nil {
log.Println("[app.Socket] blstr:", err)
return
}
defer app.hub.Unsubscribe(id)
{
b, err := json.Marshal(chatMsg{Type: "beltchat", Text: "Connected to channel: 'nightattack'"})
if err == nil {
app.hub.hub.Send(id, b)
}
}
kill := make(chan struct{})
go func() {
defer close(kill)
c.ReadMessage()
}()
tick := time.NewTicker(5 * time.Second)
defer tick.Stop()
for {
select {
case msg := <-ch:
if err := c.WriteMessage(websocket.TextMessage, msg); err != nil {
log.Println("[app.Socket] write error:", err)
return
}
case <-tick.C:
case <-kill:
return
}
}
}