Javascript 使用套接字io向特定用户发送数据

Javascript 使用套接字io向特定用户发送数据,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我有一个节点js服务器,它使用socket.io向用户发送数据。到目前为止,我可以向请求数据的用户或所有用户发送数据。但我想知道一种向其他人请求的特定用户发送数据的方法。在这个场景中,它是一个网站,您可以在其中主持游戏。因此,当另一个用户想要加入时,我想向游戏主机发送数据,告诉他们这个用户想要加入。这是我的密码 exports.Server = Server = function() { this.userId = 1; this.userName = "Guest"; }; Server.p

我有一个节点js服务器,它使用socket.io向用户发送数据。到目前为止,我可以向请求数据的用户或所有用户发送数据。但我想知道一种向其他人请求的特定用户发送数据的方法。在这个场景中,它是一个网站,您可以在其中主持游戏。因此,当另一个用户想要加入时,我想向游戏主机发送数据,告诉他们这个用户想要加入。这是我的密码

exports.Server = Server = function()
{
this.userId = 1;
this.userName = "Guest";
};

Server.prototype.initialise = function(port)
{
//Create the server using the express module
this.server = http.createServer(app);

//Declare the 'public' folder and its contents public
app.use(express.static('public'));  

//Listen to any incoming connections on the declared port and start using websockets
this.server.listen(port);
this.startSockets();
this.em = new events();

consoleLog('SERVER', 'Running on port: ' + port);
};

Server.prototype.startSockets = function()
{
//When a user connects to the server on the 'game' socket
this.socket = io.listen(this.server);

this.socket.of('game').on('connection', function(user)
    {
        //Set their usedId and username
        user.userId = this.userId;
        user.userName = this.userName + " " + this.userId;

        //Increment the user id by 1 so each user with get a unique id
        this.userId++;

        //Send a response back to the client with the assigned username and user id and initialise them
        user.emit('response', user.userId, user.userName);
        this.em.emit('initialiseUser', user.userId, user.userName);

        //Tell the server to log the user that has just connected
        consoleLog('SERVER', user.userName + ' has connected. ID: ' + user.userId)

        //Server listeners from the client

        //If the user wants to host a game, take the game settings they requested
        user.on('hostGame', function(boardSize, gameMode, gameNote)
            {       
                //Tell the app to request a new hosted game with the users information and settings
                this.em.emit('hostGame', user.userId, user.userName, boardSize, gameMode, gameNote);
            }
            .bind(this));

        //If the user wants to cancel the game they are hosting
        user.on('cancelHostGame', function()
            {   
                //Tell the app to request to cancel their game passing in the users information
                this.em.emit('cancelHostGame', user.userId, user.userName);
            }
            .bind(this));

        user.on('joinGame', function(user.id, user.userName, opponentId)
            {                       
                //Need to tell the user with the opponent Id, that the user.id wants to join
            }
            .bind(this));

        //When a user disconnects
        user.on('disconnect', function()
            {   
                //Tell the app to request to cancel the game the user was hosting
                this.em.emit('cancelHostGame', user.userId, user.userName);

                //Tell the server who disconnected
                consoleLog('SERVER', user.userName + ' has disconnected. ID: ' + user.userId)
            }
            .bind(this));
    }
    .bind(this));
})


所以你可以看到,当用户点击“加入游戏”时,它会传递他们想要加入游戏的对手的id。如果我使用user.emit,它将发送给加入的用户,如果我使用em.emit,它将发送给网站上的每个用户。我该如何将其定向到该特定id的用户?

要向游戏主机发送确认信息,首先必须管理包含已创建游戏总数的对象,并且对象的每个值将包含一个数组,其中将有连接到游戏的用户对象,因此对象结构如下所示:

var object={};
object={ game1 : [object-user1,object-user2,object-user3],
         game2 : [object-user1,object-user2,object-user3],
         game3 : [object-user1,object-user2,object-user3]
};
现在,当用户创建房间时,其对象将存储在房间下的对象中,操作如下

var object={};
io.on('connection', function(user){
    user.on('createroom',function(game){
        var array=[];
        array[0]=user;
        object[game]=array
        socket.join(data[0]);
        socket.emit('createres', data[0]);
    });
});
现在你有了一个包含游戏的对象和相应的数组,其中包含连接游戏的用户列表,现在如果你想向游戏主机发送确认信息,你可以按如下所示进行操作

user.on('joingame',function(game){
    data.game=game;
    data.user=user;
    object[game][0].emit("confirmjoin",data);
});
上面的代码是服务器端的,在上面的代码中,我们向创建游戏的用户发送确认信息,这里的对象[game][0]将返回特定游戏数组的第一个元素,因此我们可以使用该值调用客户端

这是客户端代码

user.on('confirmjoin',function(data){

    if(//allow user to join){
        user.emit('allowtojoin',data);
    }
});
上面的代码是客户端代码,在上面的代码中,如果游戏主机想允许用户加入,它将检查条件,并向服务器发送“allowjoin”发送用户对象

这是服务器端的代码,它将监听这些代码以加入游戏

user.on('allowjoin',function(data){
    data.user.join(data.game);
});
现在,您的server.js文件将包含以下内容
var object={};
io.on('连接',功能(用户){
user.on('createroom',函数(游戏){
var数组=[];
数组[0]=用户;
对象[游戏]=数组
socket.join(数据[0]);
emit('createres',数据[0]);
});
user.on('joingame',函数(game){
data.game=game;
data.user=user;
对象[game][0]。发出(“confirmjoin”,数据);
});
user.on('allowjoin',函数(数据){
数据。用户。加入(数据。游戏);
});

});为什么要标记Java?mb我输入得太快了,意思是JavaScription您是否有另一个专用于消息传递的套接字?我的回答解决了您的问题吗?@Slavik Ribz
user.on('allowjoin',function(data){
    data.user.join(data.game);
});