尝试使用断开方法从Socket对象,像这样:
io.on('connection', function(socket){
//temp delete socket
socket.disconnect();
console.log(io.sockets.connected);
socket.emit("test");
});
UPDATE:
例如,如果你的HTTP服务器会为客户端的令牌:
app.post('/api/users', function (req, res) {
var user = {
username: req.body.username
};
var token = jwt.sign(user, secret, {expiresInMinutes: 30});
res.json({token: token});
});
然后你可以重用该令牌到验证你的websocket连接。
从客户端(HTML文件)发送代码令牌将是:
socket = io.connect('http://localhost:4000', {
query: 'token=' + validToken,
forceNew: true
});
,并在服务器(socketio)的socketio授权码将是:
// here is being used a socketio middleware to validate
// the token that has been sent
// and if the token is valid, then the io.on(connection, ..) statement below is executed
// thus the socket is connected to the websocket server.
io.use(require('socketio-jwt').authorize({
secret: secret,
handshake: true
}));
// but if the token is not valid, an error is triggered to the client
// the socket won't be connected to the websocket server.
io.on('connection', function (socket) {
console.log('socket connected');
});
注意的秘密在express上使用它来生成一个标记,同样的标记也被用在socketio中间件的验证标记上。
我已经创建了一个例子,你可以看到这种验证是如何工作的,源代码是在这里:https://gist.github.com/wilsonbalderrama/a2fa66b4d2b6eca05a5d
复制它们的文件夹中,然后运行与节点server.js然后访问HTML文件从这个URL浏览器:http://localhost:4000
但首先安装模块:socket.io,快递,socketio,智威汤逊,jsonwebtoken
如果套接字仍然打开。然后它总是在倾听。在此之前,您应该确认所有连接已关闭。如果其中任何一个都没有关闭,那么客户端会听。 谢谢。 –
你不想暂时断开插座。 socket.io具有可以插入的身份验证方案,以便在未验证身份验证的情况下永远不会接受套接字。请记住,所有webSocket连接都以http请求开始,因此如果您已通过http进行身份验证,则也可以将其用于webSocket。同样从'io.sockets.connected'删除套接字不会断开套接字。 – jfriend00
我使用其余api来验证用户,以便他们可以使用该服务。据我所知,我可以验证用户,但我无法以同样的方式验证socket.io会话。我的想法是使用标记来确保使用套接字的人是经过身份验证的人 –