Open majidbigdeli opened 5 years ago
Oh @majidbigdeli I just saw it. It's the only one missing part of the neffos repo's javascript-equivalent example. It's already ready on my local machine but I didn't have enough time to test it yet (I am "designing" the new website for Iris too), I will push that feature tomorrow :)
Just to give some details:
For the browser:
As you said there is no method in the JavaScript WebSockets API for specifying additional headers for the browser to send. Therefore we have 3-4 solutions:
?header1=value1
(I've choose to proceed with that one which is already working locally but I am waiting for your respond to push)document.cookies
under the hoods can work.ws://username:password@domain/endpoint_path
protocols[]
parameter of the neffos.dial
(and raw's new WebSocket
constructor) can be used to send headers but server must be able to parse them, but there is a limitation: it must not contain a comma ,
For the nodejs side, the ws
library we use can support custom headers.
Hello @kataras
Thank you for your answer .
I agree with the first solution To push in the examples. And I use this solution.
Please add an example for the custom header in nodejs.
Thank You.
Updated
For the nodejs: You can simply use the http.request.options
literal, which can contain a headers: { 'key': 'value'}
as documented at https://nodejs.org/api/http.html#http_http_request_options_callback:
For the browser: I made it to accept the same dictionary for headers, so the same { headers: {...}}
dictionary must be used.
const conn = await neffos.dial(wsURL, {...}, {
headers: {
'X-Username': 'kataras',
}
});
Just a note for the browser-side:
The neffos.dial
function will add a prefix of X-Websocket-Header-
$key
and server-side will parse all url parameters that starts with X-Websocket-Header
and set the url param to the http.Request.Headers
field as $key, on nodejs and go client custom headers are possible by-default and are set directly.
Hope that helps @majidbigdeli
@kataras . I saw the parseHeadersAsURLParameters method . Thank you for this solution.
Your solution is very clever
I used this solution for myself already.
server := neffos.New(gobwas.DefaultUpgrader, handler)
server.IDGenerator = func(w http.ResponseWriter, r *http.Request) string {
if userID := r.Header.Get("X-Username"); userID != "" {
return userID
} else if userID := r.FormValue("X-Username"); userID != "" {
return userID
}
return neffos.DefaultIDGenerator(w, r)
}
Thank you a lot @majidbigdeli!
Yeah the solution of yours is fine and you can still use it but, you know, we needed a way to separate any user-specific url parameters to the endpoint that meant to be used as Request.URL.Query()
and not as headers, that's why we have the prefix and the parse functions on both sides (I don't want to see future issues like "why my url parameters are parsed as headers!!").
Yeah the solution of yours is fine and you can still use it but, you know, we needed a way to separate any user-specific url parameters to the endpoint that meant to be used as
Request.URL.Query()
and not as headers, that's why we have the prefix and the parse functions on both sides (I don't want to see future issues like "why my url parameters are parsed as headers!!").
Yes you are right . That's why I say that your solution is very clever.
I know that There is no method in the JavaScript WebSockets API for specifying additional headers for the client/browser to send. The HTTP path ("GET /xyz") and protocol header ("Sec-WebSocket-Protocol") can be specified in the WebSocket constructor.
The Sec-WebSocket-Protocol header (which is sometimes extended to be used in websocket specific authentication) is generated from the optional second argument to the WebSocket constructor:
The above results in the following headers:
Sec-WebSocket-Protocol: protocol
andSec-WebSocket-Protocol: protocol1, protocol2
but I have error
my index.html
I used Query String to solve this problem.
change wsURL to ws://localhost:3811/echo?UserId=123
and remove ["123"] protocol in the dial method.
Can not we have a custom header on dial?
Like golang client