2017-04-27 79 views
0

我有一个使用socket.io将数据发送给用户节点JS服务器将数据发送到特定的用户。到目前为止,我可以向请求它的用户或所有用户发送数据。但我想知道一种将数据发送给其他人请求的特定用户的方法。在这种情况下,它是一个您可以主持游戏的网站。所以当另一个用户想加入时,我想发送数据给托管该游戏的人告诉他们这个用户想加入。这里是我的代码使用插座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)); 

};

function updateGamesList(socket, hostedGames) 
{ 
//Emit the updateGameList function on the client and pass through the array of games being hosted 
socket.socket.of('game').emit('updateGameList', hostedGames); 
}; 

,这样你们可以看到,当用户点击加入游戏时,它把对手的id这就是托管他们想加入游戏。如果我做user.emit它会去加入的人,如果我做em.emit它会去每个用户在网站上。我将如何将它引导到该特定ID的用户?

+0

你为什么要标记的Java? –

+0

MB我打字太快,意味着JavaScript的 –

+0

你有另一座专门为短信? – Mike

回答

0

发送确认到主机的游戏,你首先要管理对象包含创建总游戏,以及对象的每个值将包含一个数组,其中会有用户对象连接到游戏中,使对象结构看起来,

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); 
}); 

上面的代码是用于服务器端,在这里上面的代码中我们发送确认谁创造了游戏用户,这里object [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文件将conatain像

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); 
 
    }); 
 
    user.on('allowjoin',function(data){ 
 
     data.user.join(data.game); 
 
    }); 
 
});

希望这将帮助!