How to make a server side FUNCTION execute to a particular namespace room only using socket.io, express and node.js

时间:2019-04-17 02:18:57

标签: javascript node.js express socket.io

I am working on an online multiplayer game using socket.io, express & node.js where the server detects how many users are connected to a particular room on a namespace and if then start the game. My problem is i have written the game function on the server side and when i called the function to only execute to a particular room on the namespace which has more than 2 players, the function executes across all the namespace rooms connected at the same time. I want the function to be executed only in those namespace rooms that have more than 2 users connected to them and at different intervals because my game deals with time which is round after round.I trying to use socket.io with express and node.js to implement this. Any help will be appreciated.

   //on the server side

    var express = require('express');
    var app = express();
    var fs = require('fs');
    var path = require('path');
    var server = require('http').createServer(app);
    var io = require('socket.io')(server);
    var logger = require('winston');
    var port = process.env.PORT || 4000;

    var mySocket= io.of('/games');

    //this function shows the game question 

    function showGameQuestion() {
         var question = 'How many states do we have in america?';
         return question
     }

    // this is the main game function where the game logic goes

    function playGame(gameRoom) {
       status = "chat";
       countdown = 10;
       setInterval(function() {
           countdown--;
           if (status == "chat") {
               showChat = 'you can chat now';
               if (countdown <= 0) {
                  status = "play";
                  countdown = 20;
                  showLetter = showGameQuestion();
                  mySocket.in(gameRoom).emit('go', { countdown: countdown, 
                  showMessage:showLetter, status:status });

               }
             }
             console.log(countdown);
         },1000)
   }
  //creating the socket connection

  mySocket.on('connection', function (socket) {
      socket.on('addPlayer', function (data) {
      socket.getRoomName = data.getRoomName;
      socket.join(socket.getRoomName);
      mySocket.in(socket.getRoomName).clients((error, clients) => {
      if (error) throw error;
         numUsers = clients.length;
         if(numUsers >=3){
            //calling the play game function
            playGame(socket.getRoomName);

          }
        })
     });


   //on the client side 

   var socket = io("/games");
     $("#joinBtn").click(function(){
       getRoomName = $("#roomName").val();
       socket.emit('addPlayer', getRoomName)
      })
     socket.on('go', function(data){
     $("#showQuestion").text(data.showLetter);
     })

What i expected was when a user is connected to a particular room e.g GREEN ROOM on the namespace, it should count how many users are in the room and if they are more than 2 start the game only in that particular channel on a namespace but instead the game round is being started showing the question across all channels connected on the namespace at the same time.

1 个答案:

答案 0 :(得分:0)

Change mySocket.in(gameRoom).emit('go' to

io.to(gameRoom).emit('go'