I had a look on your commit changes of episode4
.
My observations as follows:
- You're creating
hub
instance on every incoming request at stream handler. hub
instance used to keeps track connections, etc. so you're losing it on every request.
- You have removed index/home handler (may be you wanted to convert to gin handler or something, I don't know).
Now, let's bring episode4
into action. Please do following changes (as always improve it as you like). I have tested your episode4
with below changes, it's working fine.
Make /ws
handler work on server.go
:
h := newHub()
wsh := wsHandler{h: h}
r.GET("/ws", func(c *gin.Context) {
wsh.ServeHTTP(c.Writer, c.Request)
})
Remove the stream handler on connection.go
:
func stream(c *gin.Context) {
h := newHub()
wsHandler{h: h}.ServeHTTP(c.Writer, c.Request)
}
Adding index HTML handler on server.go
: (added it to test episode4 at my end)
r.SetHTMLTemplate(template.Must(template.ParseFiles("index.html")))
r.GET("/", func(c *gin.Context) {
c.HTML(200, "index.html", nil)
})