Hi there,
I'm trying to get the SockJS library for golang to work with Gin so I can do some realtime communications between rooms or channels that the clients can create.
However, I'm currently trying to test SockJS with Gin using a simple echo page that sends info to server and server sends it back for it to spit out on screen. I always end up getting a 404 on GET /echo/info. From Chrome console: GET http://localhost:8000/echo/info?t=1486141708375 404 (Not Found)
This is the console output:
[GIN] 2017/02/03 - 12:15:47 | 200 | 365.523碌s | ::1 | GET /
[GIN] 2017/02/03 - 12:15:47 | 404 | 1.526碌s | ::1 | GET /echo/info
Basic code below:
func main() {
router := gin.Default()
handler := sockjs.NewHandler("/echo/", sockjs.DefaultOptions, echoHandler)
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Main website",
}))
router.GET("/echo", gin.WrapH(handler))
router.Run(":8000")
}
func echoHandler(session sockjs.Session) {
log.Println("new sockjs session established")
for {
if msg, err := session.Recv(); err == nil {
session.Send(msg)
continue
}
break
}
log.Println("sockjs session closed")
}
and in my index template I have var sock = new SockJS('http://localhost:8000/echo') so the url address shouldn't be wrong.
Could anyone please help me with this issue?
@alphahelix00 maybe you need to add router.GET("/echo/*path", gin.WrapH(handler)) so gin handle all requests of type GET with path starting by /echo/.
@javierprovecho Is probably correct, you're trying to do two different things. Once accessing /echo and once accessing /echo/info (Which implies that both paths can be handled)
router := gin.Default()
// "/echo/" --> "/echo". remove last '/'
handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, echoHandler)
router.GET("/echo/*path", gin.WrapH(handler))
router.Run(":8000")
It works.
It works, I use Any is more convenient.
r.Any(prefix+"/*path", gin.WrapH(k8spod.CreateAttachHandler(prefix)))
Most helpful comment
@alphahelix00 maybe you need to add
router.GET("/echo/*path", gin.WrapH(handler))so gin handle all requests of type GET with path starting by/echo/.