[英]Socket.io and authentication system
我有以下代码:
io.sockets.on('connection', function (socket) {
socket.on('queue', function() {
console.log(socket.request.headers.cookie);
})
});
现在注册到我的网站是通过快递完成的,并且通过快递将https cookie添加到客户端的浏览器中。 当有人想要连接到我的时候,让我们说游戏他需要发出“队列事件”。 在那里,socket.io将读取他的cookie并检查它们是否正确。
根据这个帖子: 我可以从Socket.io访问cookie吗?
socket.request.headers.cookie
headers.cookie是禁忌,即使我不明白为什么。 为什么socket.io会将他的cookie与其他人的实时制作混淆? 我错过了什么? 我怎么能做到这一点?
如果您想要使用Socket.io进行一些身份验证,最好的方法是使用握手。
假设您已经过身份验证,服务器会向您发送令牌
客户端 :
let socket = io.connect(SERVER.socket, { query: 'token=' + token })
.on('connect', data => {
console.log('Connected to the server');
};
服务器端在套接字连接上运行中间件:
const io = require('socket.io').listen(httpServer)
// Checking for the token and handshaking with the client
.use(require('socketio-jwt').authorize({
secret: secretKey,
handshake: true
}))
// Client successfully connected (token was valid)
.on('connection', socket => {});
一些很好的教程: https : //auth0.com/blog/auth-with-socket-io/
希望它能以某种方式帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.