-
Notifications
You must be signed in to change notification settings - Fork 312
Gin compatabilty #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What does the Go program log? |
Also, print wsErr. |
Thanks for the speedy reply - I've updated the description with the server logs. |
Mhmm. I believe the issue is that gin doesn't allow writing a response before hijacking the connection the way net/http does. Gorilla works because it hijacks first, then writes the handshake response directly to the tcp connection. We could do this as well but it'd be janky. What do you get in the network tab of the browser? What does the HTTP response look like? |
Got it, what does the headers tab look like for the response? |
No response headers are shown, only the request. |
Cool, lines up with my reasoning. Looks like gin deviates from net/http for some reason. Have to call an additional WriteHeaderNow method to write the header. Opened gin-gonic/gin#2108 Will fix tonight. |
Amazing, thanks so much for the quick responses. |
Not sure if this is of use to you to fix the issue, but I just tried gobwas/ws for the heck of it and it works perfectly: package main
import (
"encoding/json"
"log"
"time"
"github.com/gin-gonic/gin"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
)
func main() {
router := gin.Default()
router.GET("/ws", handler)
router.Run(":7000")
}
func handler(c *gin.Context) {
log.Println("upgrading")
conn, _, _, err := ws.UpgradeHTTP(c.Request, c.Writer)
if err != nil {
log.Println(err)
}
log.Println("ready")
go func() {
tick := time.NewTicker(time.Second * 5)
defer tick.Stop()
defer conn.Close()
for {
select {
case <-tick.C:
log.Println("tick")
data, _ := json.Marshal(map[string]interface{}{
"msg": "hello",
})
err = wsutil.WriteServerMessage(conn, ws.OpText, data)
if err != nil {
log.Println(err)
}
}
}
}()
} |
If you add |
gohack may be helpful. https://github.com/rogpeppe/gohack |
Best to fix this in gin by making the response writer Hijack method flush any written header. |
Did I miss the oblivious solution here? Not sure how to fix that. Do I really need to use gohack, because this would be a very ugly way? Why did you close your pull request in gin-gonic/gin? |
In the end I ended up using gobwas/ws over this library, since my implementation shared above worked with Gin whilst this was awaiting a fix upstream. It's a little bit lower level than this library, but with the util package it's not too much extra work and you get a better understanding of how the underlaying protocol is working. Sorry if that's not much help for your immediate problem! |
There are a ton of unmerged PRs and issues on gin. I'd recommend against using it and I closed that PR because it didn't seem to have a chance of getting merged. However, given it's prevalence in the ecosystem, I'll add a patch here to make it work :) |
Yea my bad I meant to add a fix here but I guess it slipped. |
Master works now on Gin, will tag a release EOD :) |
Thank you! :) |
Sorry for the delay, tagged v1.8.6 today! |
I'm trying to integrate this into my existing app which is using the Gin framework, but I'm unable to establish a connection from the browser. Any ideas how I can get this working?
Server
Client
JS Error
WebSocket connection to 'ws://localhost:7000/ws' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
Server logs
The text was updated successfully, but these errors were encountered: