简体   繁体   中英

socket.io send data to matching socket's

when a user connects to my socket I add to a session map:

io.on('connection', function (socket) {
      sessionMap.set(socket.id,socket);
}

my session Map

var SessionMap = {};
module.exports = {
    set: function(key,value){
        SessionMap[key] = value;
    },
    get: function(key){
        return SessionMap[key]
    },
    delete: function(key){
        delete SessionMap[key]
    },
    all: function(){
        return SessionMap
    }
}

And also save my user socket id in a class player:

  socket.on('addPlayer-Queue', (result) => {
      sessionMap.set(socket.id,socket);
      queue.addPlayer(new Player({
        id: result.id,
        name: result.name,
        mmr: result.mmr
      }, socket.id));

And I have a function that selects two players that are connected (where I save in an array) and create a "battle" and then I wanted to send to the socket that was selected / matched for this battle the battle dice

This is my role that selects both players and creates a battle:

searching() {
    const firstPlayer = this.getRandomPlayer();

    const secondPlayer = this.players.find(
      playerTwo =>
        playerTwo.mmr < this.calculateLessThanPercentage(firstPlayer) &&
        playerTwo.mmr > this.calculateGreaterThanPercentage(firstPlayer) &&
        playerTwo.id != firstPlayer.id
    );

    if (!secondPlayer) {
      return null;
    }

    const matchedPlayers = [firstPlayer, secondPlayer];
    this.removePlayers(matchedPlayers);
    return new Match(matchedPlayers);
  }
}

And also when connecting I use a set interval to be performing this function every 1 second

But my difficulty is how I would send the data from this battle to the corresponding socket's

my relation socket with player

When a player enters my event I create a player by going through socket id And I also make a session map of this socket sessionMap.set(socket.id,socket);

my class player:

class Player {
    constructor(player,socketId) {
      this.id = player.id
      this.socketId = socketId
      this.name = player.name
      this.mmr = player.mmr
    }

  }

  module.exports = Player;
const getMatchConfigurationFor = player => {
    /* configure and return the payload notifying the player of the match */
}

const configurePlayersForNewMatch = () =>  matchedPlayers.forEach(player => 
    sessionMap.get(player.socketid)
        .broadcast.to(player.socketid)
        .emit(messageTags.MATCH_CONFIGURATION, 
            getMatchConfigurationFor(player)))

regarding where to do this work .. the single responsibility principle says that a function should have a singular clear purpose. So the search method should search for matching players, not configure the match. You should do this work in another function that is called while configuring the match, which itself is called after the search returns successfully. I've provided the wrapper function for that here: it is written in a fashion to expect the relevant pieces are in scope. You could rewrite it as a proper function with parameters if you prefer.

This is a work in progress solution for Felipe, posted by request.

After a match is found, you'd probably want to emit a MatchFound object to both clients detailing information about the match (including information about their opponent). Once a client gets this, you can initiate anything the client needs for a match (load a level, display names, or a lobby).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM