-
Notifications
You must be signed in to change notification settings - Fork 5
/
websocket.go
140 lines (122 loc) · 3.55 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package fir
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"github.com/golang/glog"
"github.com/gorilla/websocket"
"github.com/livefir/fir/internal/dom"
)
func onWebsocket(w http.ResponseWriter, r *http.Request, route *route) {
channel := route.channelFunc(r, route.id)
if channel == nil {
glog.Errorf("[onWebsocket] error: channel is empty")
http.Error(w, "channel is empty", http.StatusUnauthorized)
return
}
conn, err := route.websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
return
}
defer conn.Close()
ctx := context.Background()
// eventSender
go func() {
for event := range route.eventSender {
eventCtx := RouteContext{
event: event,
request: r,
response: w,
route: route,
domPatcher: dom.NewPatcher(),
session: route.session,
}
glog.Errorf("[onWebsocket] received server event: %+v\n", event)
onEventFunc, ok := route.onEvents[strings.ToLower(event.ID)]
if !ok {
glog.Errorf("[onWebsocket] err: event %v, event.id not found\n", event)
continue
}
handleOnEventResult(onEventFunc(eventCtx), eventCtx, publishPatch(ctx, eventCtx))
}
}()
// subscribers
subscription, err := route.pubsub.Subscribe(ctx, *channel)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer subscription.Close()
go func() {
for patchset := range subscription.C() {
go writePatchOperations(conn, *channel, route.template, patchset)
}
}()
if route.developmentMode {
// subscriber for reload operations in development mode. see watch.go
reloadSubscriber, err := route.pubsub.Subscribe(ctx, devReloadChannel)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer reloadSubscriber.Close()
go func() {
for patchset := range reloadSubscriber.C() {
go writePatchOperations(conn, devReloadChannel, route.template, patchset)
}
}()
}
loop:
for {
_, message, err := conn.ReadMessage()
if err != nil {
log.Println("[onWebsocket] c.readMessage error: ", err)
break loop
}
var event Event
err = json.NewDecoder(bytes.NewReader(message)).Decode(&event)
if err != nil {
glog.Errorf("[onWebsocket] err: parsing event, msg %s \n", string(message))
continue
}
if event.ID == "" {
glog.Errorf("[onWebsocket] err: event %v, field event.id is required\n", event)
continue
}
eventCtx := RouteContext{
event: event,
request: r,
response: w,
route: route,
domPatcher: dom.NewPatcher(),
session: route.session,
}
glog.Errorf("[onWebsocket] received event: %+v\n", event)
onEventFunc, ok := route.onEvents[strings.ToLower(event.ID)]
if !ok {
glog.Errorf("[onWebsocket] err: event %v, event.id not found\n", event)
continue
}
handleOnEventResult(onEventFunc(eventCtx), eventCtx, publishPatch(ctx, eventCtx))
}
}
func writePatchOperations(conn *websocket.Conn, channel string, t *template.Template, patchset dom.Patchset) error {
message := dom.MarshalPatchset(t, patchset)
if len(message) == 0 {
err := fmt.Errorf("[writePatchOperations] error: message is empty, channel %s, patchset %+v", channel, patchset)
log.Println(err)
return err
}
glog.Errorf("[writePatchOperations] sending patch op to client:%v, %+v\n", conn.RemoteAddr().String(), string(message))
err := conn.WriteMessage(websocket.TextMessage, message)
if err != nil {
glog.Errorf("[writePatchOperations] error: writing message for channel:%v, closing conn with err %v", channel, err)
conn.Close()
}
return err
}