Skip to content
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

Websocket not working with router #26

Closed
fadeojo opened this issue Nov 25, 2017 · 4 comments
Closed

Websocket not working with router #26

fadeojo opened this issue Nov 25, 2017 · 4 comments

Comments

@fadeojo
Copy link

fadeojo commented Nov 25, 2017

I tried using the websocket library(melody) as well as other libraries but they don't work with abcweb. The websocket connection never opens. I also tried melody in a simple setup with chi router and it worked.

package main

import (
	"github.com/go-chi/chi"
	"gopkg.in/olahol/melody.v1"
	"net/http"
)

func main() {
	r := chi.NewRouter()
	m := melody.New()

	r.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
		m.HandleRequest(w, r)
	})


	m.HandleMessage(func(s *melody.Session, msg []byte) {
		m.Broadcast(msg)
	})

	http.ListenAndServe(":3000", r)
}

I add the code below to the router in my abcweb router file and it doesn't work. Please can I get help on this issue?

router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
		m.HandleRequest(w, r)
	})

My Repo: https://github.com/fadeojo/brito

@nullbio
Copy link
Member

nullbio commented Dec 1, 2017

Hi. What happens when you navigate to /ws? Have you put in some debug statements to see if that router handler is being hit? Are you receiving any errors from the HandleRequest call? What do you mean by "it doesn't work"? You'll need to splatter debug statements around to try and figure out what's happening. I see no reason why abcweb would not be compatible with this off the top of my head.

@fadeojo
Copy link
Author

fadeojo commented Dec 3, 2017

Hi @nullbio I didn't add any error logs because I was getting very little. I was hoping the provided repo would give an easier way to reproduce the issue.

2017-12-02T21:24:00.012-0500    INFO    abcserver/server.go:81  starting http listener  {"bind": ":4000"}
2017-12-02T21:24:11.102-0500    INFO    abcmiddleware/log.go:81 http request    {"request_id": "MacBook-Pro.local/8bleKNoWvD-000001", "status": 500, "method": "GET", "uri": "/ws", "tls": false, "protocol": "HTTP/1.1", "host": "localhost:4000", "remote_addr": "[::1]:62891", "size": 22, "elapsed": "82.21µs"}

I get the above when I try to connect via websocket ws://localhost:4000/ws. Please can you pull my repo and try it out if you don't mind? Thank you for taking the time.

@nullbio
Copy link
Member

nullbio commented Dec 5, 2017

Hi @fadeojo, so I did a bit of digging. First thing is you want to wrap your handler using the error manager middleware so that you can log errors as they occur. This is the main reason why your output is not helpful in diagnosing the problem, I believe. Doing so like this, gives the following for me:

	router.Get("/ws", e(func(w http.ResponseWriter, r *http.Request) error {
		return m.HandleRequest(w, r)
	}))
~/g/s/g/f/brito (master|✚1) $ abcweb dev
No gulpfile.js present, skipping gulp watch
refresh: 2017/12/05 17:21:04 === Rebuild on: :start: ===
refresh: 2017/12/05 17:21:04 === Running: go build -v -i -o /tmp/britobuild (PID: 16458) ===
github.com/fadeojo/brito
refresh: 2017/12/05 17:21:05 === Building Completed (PID: 16458) (Time: 558.183954ms) ===
refresh: 2017/12/05 17:21:05 === Running: /tmp/britobuild (PID: 16500) ===
dir
/home/pat/gop/src/github.com/fadeojo/brito/ui
2017-12-05T17:21:05.385+1000	INFO	abcserver/server.go:81	starting http listener	{"bind": ":4000"}
2017-12-05T17:21:12.393+1000	ERROR	abcmiddleware/errors.go:146	request error	{"request_id": "jarvis/mV6RShhaLG-000001", "method": "GET", "uri": "/ws", "tls": false, "protocol": "HTTP/1.1", "host": "localhost:4000", "remote_addr": "[::1]:55822", "error": "websocket: not a websocket handshake: 'upgrade' token not found in 'Connection' header"}
github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware.(*errorManager).Errors.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware/errors.go:146
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/go-chi/chi.(*Mux).routeHTTP
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/go-chi/chi/mux.go:428
github.com/fadeojo/brito/vendor/github.com/go-chi/chi.(*Mux).(github.com/fadeojo/brito/vendor/github.com/go-chi/chi.routeHTTP)-fm
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/go-chi/chi/mux.go:372
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/go-chi/chi/middleware.NoCache.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/go-chi/chi/middleware/nocache.go:54
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware.Middleware.Zap.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware/log.go:27
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware.Middleware.Recover.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware/recover.go:59
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware.Middleware.RequestIDLogger.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/volatiletech/abcweb/abcmiddleware/log.go:43
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/go-chi/chi/middleware.RequestID.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/go-chi/chi/middleware/request_id.go:68
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/volatiletech/refresh/refresh/web.ErrorChecker.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/volatiletech/refresh/refresh/web/web.go:23
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/rs/cors.(*Cors).Handler.func1
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/rs/cors/cors.go:200
net/http.HandlerFunc.ServeHTTP
	/usr/local/go/src/net/http/server.go:1918
github.com/fadeojo/brito/vendor/github.com/go-chi/chi.(*Mux).ServeHTTP
	/home/pat/gop/src/github.com/fadeojo/brito/vendor/github.com/go-chi/chi/mux.go:81
net/http.serverHandler.ServeHTTP
	/usr/local/go/src/net/http/server.go:2619
net/http.(*conn).serve
	/usr/local/go/src/net/http/server.go:1801
2017-12-05T17:21:12.393+1000	DEBUG	abcserver/server.go:28	http: multiple response.WriteHeader calls

2017-12-05T17:21:12.393+1000	INFO	abcmiddleware/log.go:81	http request	{"request_id": "jarvis/mV6RShhaLG-000001", "status": 500, "method": "GET", "uri": "/ws", "tls": false, "protocol": "HTTP/1.1", "host": "localhost:4000", "remote_addr": "[::1]:55822", "size": 1037, "elapsed": "424.288µs"}

Now, I don't really know much about the websocket protocol, but I presume this is because I'm trying to access it in the browser (which is naturally trying to send a regular HTTP request). Hopefully this is enough to get you pointed in the right direction?

@oadeojo
Copy link

oadeojo commented May 11, 2018

I couldn't get this working and decided to build my project using other technologies. Please close this issue. thanks.

@nullbio nullbio closed this as completed May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants