Socket用Node.js发出ajax接收到的数据



服务器端:我通过ajax接收用户列表,我试图通过套接字发出它,但仅对相同的客户端。像这样

io.sockets.emit("users list", {users: users, me : req.me});//This work, but to all users
socket.emit("users list", {users: users, me : req.me}); //Im trying something like this

的代码
//Receive user list
    app.post('/', function(request, response){
        response.setHeader('Access-Control-Allow-Origin', 'http://localhost'); //To allow response
        var req = JSON.parse(request.body.data);
        if(typeof req != "object") {
            response.send({status: false, msg: "Error on load user list"});
        } else {
            socket.id = req.me.id;
            req.users.data.forEach(function(user) {
                if(users[user.id] == null)
                    users[user.id] = user;
            });
            //io.sockets.emit("users list", {users: users, me : req.me});//This work, but to all users
            socket.emit("users list", {users: users, me : req.me}); //Im trying something like this
        }
        response.send({status: true, msg: ""});// echo the result back
    });

我还想知道是否有最好的方法来做到这一点

你可以尝试发送数据到一个特定的套接字,像这样:

io.sockets.socket (socket.id)。发出("用户列表",{用户:用户,我:req.me});

相关内容

  • 没有找到相关文章

最新更新