I'm using one of the golang samples to build a WebSocket server. According to the samples the struct Client
access websocket.Conn
here and websocket.Conn
has a Request
method returning *http.Request
.
My idea is to subscribe the user to different channels depending on the subdomain they use to enter the app.
So is it possible to get the full URL the user hit when subscribed to the websocket? Using the sample code I thought ws.Conn.Request().URL.Host
would give me something like ws://channel1.lvh.me:8080/sync
but instead I'm getting the path: "/chat".
Is it possible to get the full URL the user used to connect to the websocket? Thanks in advance.
func (*Conn) Request
returns *http.Request
func (ws *Conn) Request() *http.Request
Checking the http.Request
from the http
package you will observe that it has a URL *url.URL
type. This means that you should get the full url with:
ws.Request().URL.Path
Examine the host header to get the requested host:
host := c.Request().Host
The Request.URL field is parsed from the HTTP request URI. For most requests, the request URI contains path and query only.
An application can reconstruct a full URL from the host header and knowledge of the protocols the application is serving. For example, if the application is listening for HTTP requests (or WS request that use HTTP for handshake), the full URL is:
u := *c.Request().URL
u.Host = c.Request().Host
u.Scheme = "http"