Skip to content

Instantly share code, notes, and snippets.

@akaDPR
Created February 22, 2020 17:14
Show Gist options
  • Save akaDPR/7f9dfedc23a31d9a0977343d22fa60ea to your computer and use it in GitHub Desktop.
Save akaDPR/7f9dfedc23a31d9a0977343d22fa60ea to your computer and use it in GitHub Desktop.
router.HandleFunc("/ws/{rname}", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
/*
Authenticate user with custom-headers
After authentication , Get the action from client
*/
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
vars := mux.Vars(r)
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
m := "Unable to upgrade to websockets"
http.Error(w, m, http.StatusBadRequest)
return
}
vars["rname"] = string(vars["rname"])
if err != nil {
log.Println(err)
return
}
c := &connection{send: make(chan []byte, 256), ws: ws}
/*the subscription struct will hold the ws pointer , associate rooms*/
s := subscription{c, vars["rname"]}
go h.run(db)
h.register <- s
go s.writePump()
s.readPump()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment