Skip to content

Instantly share code, notes, and snippets.

@mosleim
Created February 12, 2020 00: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 mosleim/3a3894813e012b601445e3e170013178 to your computer and use it in GitHub Desktop.
Save mosleim/3a3894813e012b601445e3e170013178 to your computer and use it in GitHub Desktop.
coba websocket ke nats
func (h *Handler) dksocket(c *gin.Context) {
token := h.extractClaim(c)
conn, err := wsupgrader.Upgrade(c.Writer, c.Request, nil)
nc, _ := nats.Connect(nats.DefaultURL)
nc.Subscribe(token.UserID, func(m *nats.Msg) {
conn.WriteMessage(1, m.Data) // meneruskan subcribe ke websocket. potensi panic ga??
})
nc.Subscribe("pengumuman", func(m *nats.Msg) {
conn.WriteMessage(1, m.Data) // meneruskan subcribe ke websocket. potensi panic ga??
})
for {
t, msg, err := conn.ReadMessage()
fmt.Println(t, msg, err)
conn.WriteMessage(t, msg)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment