Wednesday 26 April 2017

Send data to a specific user using socket io

i have a node js server which uses socket.io to send data to users. So far i can send data to the user that requested it, or all users. But i want to know a way to send data to a specfic user requested from someone else. In this scenario its a website where you can host a game. So when another user wants to join, i want to send data to the person hosting that game telling them this user wants to join. Here is my code

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

};

So u can see when a user clicks join game, it passes the id of the opponent thats hosting the game they want to join. If i do user.emit it will go to the person joining and if i do em.emit it will go to every user on the website. How would i just direct it to a user of that specific id?



via Slavik Ribz

No comments:

Post a Comment