I'm trying to connect one nodejs client to a nodejs server through websockets, but setup is done using http to login to the server. This is so that the Main server can "recognize" the client through its cookies. Here is how my session is stored:
const request = require('request');
var cookieJar = request.jar();
// an http request to login using the cookieJar
Where in the Websocket initialization can I put the cookies in?
const WebSocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path', {
perMessageDeflate: false
//cookie: cookieJar
});
Clarification:
Where in this code can I put an option for sending cookies as well?
const ws = new WebSocket('ws://www.host.com/path', {
headers: {
Cookie: 'foo=bar; baz=qux'
}
});
Closing as answered, discussion can continue if needed.
this should be documented
Feel free to make PR with a documentation improvement.
It might be not obvious but it's already documented. https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options
Any other option allowed in http.request() or https.request().
having the option to reuse request's cookieJar would be a nice feature (path,domains,expire,...) ? it would ease the pain of parsing and handling of cookie to build a raw low level header
Most helpful comment