我使用的是socket.io V1.2.1&试图遵循official Socket.io authorization wiki page,但它似乎有点过时(可能是为0.9版编写的),所以在做了一些更改之后,这里是我启动服务器和代码的代码。配置身份验证:
server = http.createServer(app)
io = require('socket.io')(server)
global.io = io
global.liveTopicIo = io.of "/live-topic"
server.listen(3100)
io.set 'authorization', (handshakeData, callback)->
# doing some authentication logic here
# & reading 'currentUser'
if isAuthenticated
handshakeData.user = currentUser
callback null, true
else
callback null, false
liveTopicIo.on "connection", (socket) ->
console.log 'user: ', socket.handshake.user
console.log 'a user connected'
上一个代码打印以下内容
user:undefined
用户已连接
那么,为什么我无法在handshake
数据中添加参数? &安培;如何修复此问题以添加当前用户?
答案 0 :(得分:0)
基于the migration notes from 0.9 to 1.x,授权不再依赖于set
函数,而应该是正常的快速中间件,所以它应该是这样的:
liveTopicIo.use (socket, next)->
# doing some authentication logic here based on the info
# from socket.handshake & reading 'currentUser'
if isAuthenticated
socket.handshake.user = currentUser
next()
else
next(new Error("Access Denied"))