Skip to content

Instantly share code, notes, and snippets.

@matti
Created November 28, 2019 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matti/ce8480286966c50b69baa8372c73b544 to your computer and use it in GitHub Desktop.
Save matti/ce8480286966c50b69baa8372c73b544 to your computer and use it in GitHub Desktop.
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
logFatalError(err)
clients[conn] = redisClient.Subscribe(*channelName)
n, err := redisClient.Publish(*channelName, "x").Result()
browsers := strconv.FormatInt(n, 10)
for client := range clients {
logFatalError(err)
err = client.WriteMessage(websocket.TextMessage, []byte(browsers))
if err != nil {
log.Println("client gone")
delete(clients, client)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment