Looking at the doc, we can read cookies from a server, with upgradeReq, but from a client I don't see how to set cookies before making the connection
In the node.js variant you can supply an options object with a headers key where you can add the Cookie header your self.
What is the node.js variant? still in ws? Could you show a code example please?
I think he means in node WebSocket server you can write Set-Cookie header like that:
const wss = new WebSocketServer({
port: 9000
});
wss.on("headers", function(headers) {
headers["set-cookie"] = "SESSIONID=" + crypto.randomBytes(20).toString("hex");
console.log("handshake response cookie", headers["set-cookie"]);
});
wss.on("connection", function(ws) {
console.log("connection request cookie: ", ws.upgradeReq.headers.cookie);
});
+1 set cookie in node js ? ws
Hello!
I need to set cookie. This decision is not work ((( ! *
*
wss.on("headers", function(headers) {
headers["set-cookie"] = "SESSIONID=" + crypto.randomBytes(20).toString("hex");
console.log(headers["set-cookie"]);
});
In console:
[ 'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
'Sec-WebSocket-Accept: vGkfnvhYceS53qvrDOjE8Clteq0=',
'Sec-WebSocket-Extensions: permessage-deflate',
'Set-Cookie': 'repa=1461679469325; domain=""; path=/' ]
But in client cookie not setted, in ws.upgradeReq.headers (on connection event) is not setted!
Is there any way to set cookie ( ?
What may be wrong?
PS: I use WSS
@eugen35 You've got something else overriding the Set-Cookie header with the cookie "repa" (and a timestamp as value) before sent by the server
No its my experiments with sets of cookie. Any way cookie "repa" is felt on client and in response
I find solution:
web_socket_server.on('headers', (headers) => {
headers.push('Set-Cookie: ' + cookie_parser.serialize('testing', 'test')});
});
headers is a Array, not Object, because solution @mems , don`t work.
UPD: cookie_parser is a npm module: https://www.npmjs.com/package/cookie
code example
var WebSocket = require('ws');
var cookie = require('cookie');
var ws = new WebSocket(
'http://localhost/auth',
[],
{
'headers': {
'Cookie': cookie.serialize('id', '496E66DD')
}
}
);
@whiler This worked for me when using ws as a web socket client in nodejs. Can you please provide a link to the documentation?
@brenthmiras you might not need this any more, but here's the link you want: https://github.com/websockets/ws/blob/db1486492a00f5cd6b43bed06773668d54994593/doc/ws.md#new-websocketaddress-protocols-options
code example
var WebSocket = require('ws'); var cookie = require('cookie'); var ws = new WebSocket( 'http://localhost/auth', [], { 'headers': { 'Cookie': cookie.serialize('id', '496E66DD') } } );
Can I know, how to receive or get, the cookie which be set in server side?
The server side code as below
var responseHeader = make(http.Header)
var cookie = &http.Cookie{
Name: "foo",
Value: "bar",
Secure: true,
}
responseHeader.Add("Set-Cookie", cookie.String())
What exact way, I can get this cookie, from client side
Client side code like below:
if (window["WebSocket"]) {
let self = this
var wss = new WebSocket("wss://xxx.com/api/v5/wss/v3/wss")
wss.addEventListener("open", onOpenFunc)
function onOpenFunc(event) {
//how to get the cookie here?
}
Most helpful comment
code example