Skip to content
Permalink
Browse files

add websocket channel for notification of new channels

  • Loading branch information...
cbrake committed Jan 14, 2019
1 parent 6891aee commit ca91e094ec395bfe145b8ad693de7e14939c94f9
Showing with 9 additions and 3 deletions.
  1. +9 −3 api/websocket.go
@@ -18,11 +18,14 @@ var upgrader = websocket.Upgrader{
type WebsocketHandler struct {
clients map[*websocket.Conn]bool
lock *sync.RWMutex
newConn chan<- bool
}

func (h *WebsocketHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fmt.Println("Websocket handler")

h.newConn <- true

ws, err := upgrader.Upgrade(rw, req, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
@@ -53,8 +56,11 @@ func (h *WebsocketHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
h.lock.Unlock()
}

// NewWebsocketHandler returns a new websocket handler
func NewWebsocketHandler(wsTx chan []byte) http.Handler {
// NewWebsocketHandler returns a new websocket handler. The wsTx channel is
// used to send data out the websocket, and the newConn channel is used to
// signal back to the caller that a new client has connected, and the initial
// data set needs to be sent over.
func NewWebsocketHandler(wsTx <-chan []byte, newConn chan<- bool) http.Handler {
clients := make(map[*websocket.Conn]bool)
var lock sync.RWMutex
go func() {
@@ -76,5 +82,5 @@ func NewWebsocketHandler(wsTx chan []byte) http.Handler {
}
}
}()
return &WebsocketHandler{clients: clients, lock: &lock}
return &WebsocketHandler{clients: clients, lock: &lock, newConn: newConn}
}

0 comments on commit ca91e09

Please sign in to comment.
You can’t perform that action at this time.