【发布时间】:2019-04-23 11:01:25
【问题描述】:
在我的客户上,我这样做:
useEffect(() => {
socket.emit('login', myLoginCode)
}, [])
在服务器端我之前是这样做的:
app.get('login', (req, res) => {
const cookieProtected = {
maxAge: 946080000000,
httpOnly: true,
secure: true,
sameSite: true
}
res.cookie('id', login, cookieProtected)
res.cookie('session', encryptSession, cookieProtected)
res.cookie('logged', 'true', {
maxAge: 946080000000,
secure: true,
sameSite: true
})
})
但是我怎样才能对套接字做同样的事情呢?
socket.on('login', loginCode => {
// How to to place cookies in user browser from here?
}
也许有办法用 socket.emit 发送标头?
【问题讨论】:
-
io.on('connection', async socket => { try { socket.handshake.headers.cookie = 'test=1; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/' 是的,它不起作用。