Skip to content

Commit 3f741e8

Browse files
committed
Fix accepting WS connections from browsers.
1 parent 44fc7df commit 3f741e8

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

zebrascand/server.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,26 @@ func (s *Server) Serve() {
3939
s.broadcastChan = make(chan []byte)
4040
s.subscribeChan = make(chan chan []byte)
4141

42-
upgrader := websocket.Upgrader{}
42+
upgrader := websocket.Upgrader{
43+
CheckOrigin: func(r *http.Request) bool {
44+
// FIXME: for now, accept any origin
45+
return true
46+
},
47+
}
4348

4449
route := http.ServeMux{}
45-
4650
route.HandleFunc("/scan", func(w http.ResponseWriter, r *http.Request) {
51+
log := log.WithFields(log.Fields{
52+
"remoteAddr": r.RemoteAddr,
53+
})
54+
4755
conn, err := upgrader.Upgrade(w, r, nil)
4856
if err != nil {
49-
log.WithFields(log.Fields{
50-
"remoteAddr": conn.RemoteAddr(),
51-
}).Error("failed to upgrade to WS: ", err)
57+
log.WithError(err).Error("server: failed to upgrade to WS")
5258
return
5359
}
5460

55-
log.WithFields(log.Fields{
56-
"remoteAddr": conn.RemoteAddr(),
57-
}).Info("accepted WS connection")
61+
log.Info("server: accepted WS connection")
5862

5963
go s.socketEventLoop(conn)
6064
})
@@ -71,15 +75,12 @@ func (s *Server) Serve() {
7175
func (s *Server) broadcastLoop() {
7276
subs := make(map[chan []byte]bool)
7377
for {
74-
log.Debug("enter broadcastLoop")
7578
select {
7679
case sub := <-s.subscribeChan:
77-
log.Debug("register subscriber ", sub)
7880
subs[sub] = true
7981

8082
case msg := <-s.broadcastChan:
8183
for sub := range subs {
82-
log.Debug("broadcast subscriber ", sub)
8384
sub <- msg
8485
}
8586
}

0 commit comments

Comments
 (0)