我正在使用socket.io示例进行聊天项目。我想为聊天应用程序添加密码保护。这只是简单的身份验证。如果用户输入了错误的密码,则服务器将断开该用户的连接。
客户端代码:
SELECT t2.* FROM [tableA] t1
INNER JOIN [tableA] t2
ON {fn timestampdiff(SQL_TSI_DAY, t1.DateTimeColumn, t2.DateTimeColumn)} = t1.[Shift]
服务器端代码:
var ctx = document.getElementById("myPieChart");
var myPieChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["Astronomica", "Deuteronic", "Floral", "Galactic","Celestrial","Heliosphere","Jupiter","Interstella","Koronis","Eclipse,"Borealis","Lunatic"],
datasets: [{
data: [12.21, 15.58, 11.25, 8.32],
backgroundColor: ['#007bff', '#dc3545', '#ffc107', '#28a745'],
}],
},
});
close()方法不起作用。断开连接的用户仍然能够接收来自其他用户的消息。那么如何正确地做到这一点呢?谢谢。
答案 0 :(得分:0)
假设您正在使用以下方式创建ID(服务器端):
io.engine.generateId = (req) => {
return custom_id++; // custom id must be unique
}
您可以使用.disconnect()
关闭特定的连接,并可以使用socket.id
调用ID
socket.on('password', function (msg) {
if (msg !== '123321') {
io.emit('system info', 'Wrong password... Disconnecting ' + username+'...');
io.sockets.connected[socket.id].disconnect();//<-- in this case it's just a number
}
});