-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Is mux supposed to work with WebSocket? #29
Comments
I don't see any obvious reasons why it should not work. Can you provide any more details? Are you sure the client is going specifically to the /ws URL and not a sub-path thereof? The gorilla mux example will return 404 for anything that's not exactly /ws or / |
Even r.PathPrefix("/ws").Handler(ws.Server{
Handshake: s.handleWSHandshake,
Handler: s.handleWSConnection,
}) is not working. It's giving me GET /ws -> 404. |
Do you have the actual code available somewhere, to provide a runnable example? |
Gorilla works fine with /ws handler for us. See https://github.com/strukturag/spreed-speakfreely/blob/master/src/app/spreed-speakfreely-server/main.go#L309 - i guess this can be closed. |
Yeah, I've seen other people using it successfully. Please reopen if there is a specific example of it not working. |
Can anyone please share a working example of WebSocket with gorilla mux? |
mux doesn’t “interfere” with WebSockets: it will route the initial
handshake request to the handler you define, just like the standard library.
Taking the gorilla/websocket “chat” example & adapting for mux:
https://github.com/gorilla/websocket/blob/master/examples/chat/main.go
All I do is create a Router and replace http.HandleFunc with mux.Router’s
HandleFunc methods.
```
func main() {
flag.Parse()
hub := newHub()
go hub.run()
r := mux.NewRouter() // Create a mux instance
r.HandleFunc("/", serveHome)
r.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
err := http.ListenAndServe(*addr, r)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
```
…On Wed, Jul 25, 2018 at 12:41 AM Bipin ***@***.***> wrote:
Can anyone please share a working example of WebSocket with gorilla mux?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#29 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABIcH_vnzez145DqfeZOeQVfNZDdihAks5uKCEqgaJpZM4AyeGk>
.
|
(Excuse the formatting, I typed this on my phone)
…On Wed, Jul 25, 2018 at 6:47 AM Matt S ***@***.***> wrote:
mux doesn’t “interfere” with WebSockets: it will route the initial
handshake request to the handler you define, just like the standard library.
Taking the gorilla/websocket “chat” example & adapting for mux:
https://github.com/gorilla/websocket/blob/master/examples/chat/main.go
All I do is create a Router and replace http.HandleFunc with mux.Router’s
HandleFunc methods.
```
func main() {
flag.Parse()
hub := newHub()
go hub.run()
r := mux.NewRouter() // Create a mux instance
r.HandleFunc("/", serveHome)
r.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
err := http.ListenAndServe(*addr, r)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
```
On Wed, Jul 25, 2018 at 12:41 AM Bipin ***@***.***> wrote:
> Can anyone please share a working example of WebSocket with gorilla mux?
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <#29 (comment)>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AABIcH_vnzez145DqfeZOeQVfNZDdihAks5uKCEqgaJpZM4AyeGk>
> .
>
|
Okay thank my friend. |
Just had the same issue. In my case, it was not related to the library. I just missed the fact that I had another route before this one which took the request:
Changing lines order solved the issue for me 🙈 |
Hi,
I am using mux together with WebSocket in my app and I am constantly getting 404. When I replace mux with http.ServeMux, everything starts working. I register the handler with
and
is the working code.
I am asking because I saw people using it on the internet, so I don't know if it broke recently or so...
Thanks for the reply!
Regards,
Ondra Kupka
The text was updated successfully, but these errors were encountered: