我在删除 Socket.IO 中的用户时遇到问题,在他们的浏览器关闭user
后,数组中的数组users[]
不会被删除也不会更新,我还想更新客户端的用户。你能帮我以编程方式删除用户吗?这是我的代码家伙,我希望你能帮助我下面提供的代码。我已经在网上浏览了如何删除用户,但我无法真正让它发挥作用。提前谢谢各位。
这是我的server.js:
var redis = require('redis');
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var publish = redis.createClient();
var subscribe = redis.createClient();
server.listen(8080);
var users = [];
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('adduser', function (user) {
users[user] = user;
console.log(users);
});
socket.on('disconnect', function (user) {
delete users[user] = user;
io.sockets.emit('update', user);
// delete users[user] = users;
// socket.emit('update', users);
console.log(users)
});
socket.on('update', function () {
users[user] = user;
console.log('Current users: ', users);
});
});
这是我的client.html:
<html>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('connect', function(){
socket.emit('adduser', prompt("What's your name?"));
});
</script>
<div style="float:left;width:150px;border-right:2px solid black;height:510px;padding:10px;overflow:scroll-y;text-align:center;">
<b>Users</b>
<div id="users"></div>
</div>
</html>
再次感谢大家,我希望你能帮助我:)