简体   繁体   English

从客户端向所有房间发出功能一次,socket.io

[英]emit function from client side to all room only once, socket.io

I am a newbie with socket.io and I have a hard time figuring out how to correctly use server and client.我是 socket.io 的新手,我很难弄清楚如何正确使用服务器和客户端。 So, I have a function on my client side that listens to the server to get an array of questions to display it to the room.所以,我在客户端有一个函数,它监听服务器以获取一系列问题以将其显示到房间。

The idea is to create a room with several clients connected, and to emit to one room a function from the client side (only once, as I want to display the information to all the room).这个想法是创建一个连接了多个客户端的房间,并从客户端向一个房间发出功能(仅一次,因为我想向所有房间显示信息)。 I really need this function on my client side as I have approximately 100 lines of code that concerns the front.我真的需要在我的客户端使用这个功能,因为我有大约 100 行涉及前端的代码。 Also my server needs to emit its array to the client.我的服务器还需要将其数组发送给客户端。 My problem is, since it is client side, when I emit it to the server it will be emitted x times the number of clients inside the room.我的问题是,因为它是客户端,所以当我将它发送到服务器时,它会被发送 x 倍于房间内的客户端数量。 And I wish to emit it only once.我希望只发射一次。

To have a concrete example : My server has an object of array which is passed to the clients in a room so they can display it.举一个具体的例子:我的服务器有一个数组对象,它被传递给房间中的客户端,以便他们可以显示它。 Here is the function in the client :这是客户端中的功能:

//I have shorten the function so it is not too much but you can still grasp how I need it in my client
socket.on('getNewQuestion', (currentQuestion) => {

init();
questionNumber.innerHTML = "Question " + countQuestion;
questionParams.id = currentQuestion.questionId;
questionParams.questionTxt = currentQuestion.question;
questionParams.reponse = JSON.parse(currentQuestion.reponse);
questionParams.lien = currentQuestion.lien;
questionParams.type = currentQuestion.type;

//Display question text
questionTxt.innerHTML = currentQuestion.question;

//Display different informations according to the question's type
switch (currentQuestion.type) {
    case "enigme":
        var getReponseLength = JSON.parse(currentQuestion.reponse)[0].length;
        questionType.innerHTML = "find the answer to this question with " + getReponseLength + " letters";

        break;

    case "anagramme":

        questionType.innerHTML = "find a word usign all these letters :";
        const html = [];
        //Parse string to create array of chars
        var questionParse = JSON.parse(currentQuestion.question);
        //Gets the characters of the string and shuffles it 
        for (let i = questionParse.length - 1; i >= 0; i--) {
            const randLetters = Math.floor(Math.random() * (i + 1));
            [questionParse[i], questionParse[randLetters]] = [questionParse[randLetters], questionParse[i]];
            html.push(`<div>${questionParse[i]}</div>`);//display letter
        }
        currentQuestion.question = questionParse;
        questionTxt.innerHTML = html.join(" ");
        break;
}
countQuestion++;
});

Then I emit it in my server to get the object of question然后我在我的服务器中发出它以获取问题对象

  socket.on('callGetNewQuestion', (roomCode) => {
    //Get a random question from the object
    const questionCalled = questions.getQuestionsList(roomCode)[Math.floor(Math.random() * questions.getQuestionsList(roomCode).length)];
    io.in(roomCode).emit('getNewQuestion', questionCalled); //emit the random question to the client
    questions.removeQuestion(questionCalled.questionId); //remove it so it doesn't appear twice
  });

And I am emitting back in my client (which I am not sure if it is good practice but I don't have any idea on how to listen to the object otherwise)而且我正在向我的客户发出回音(我不确定这是否是好的做法,但我不知道如何收听该对象)

socket.on('startGame', () => {
    socket.emit('callGetNewQuestion', userParams.roomCode);
    timerCountdownGame();
});

The problem with this code is : as it is called from the client side, if there is two clients in the room it is called twice, if there are three clients, it is called three times, so when I start the game it will pass from question 1 to question 3 (if number of clients is three).这段代码的问题是:因为它是从客户端调用的,如果房间里有两个客户端,它会被调用两次,如果有三个客户端,它会被调用三次,所以当我开始游戏时它会通过从问题 1 到问题 3(如果客户数量是三个)。 I hope what I am saying makes sense, if not do not hesitate to tell me我希望我说的有道理,如果没有,请毫不犹豫地告诉我

Does anyone know how I can correct it ?有谁知道我该如何纠正它?

The main problem you are having is that the server behaviour is driven by the client, which should be the opposite...您遇到的主要问题是服务器行为是由客户端驱动的,这应该是相反的......

Why should your client request a new question?为什么您的客户要提出新问题? Your server should send the new question whenever it is available.您的服务器应在可用时发送新问题。 If really you need to have your client request question, you should store the current question in your room and send the currentQuestion, not generate it.如果您确实需要客户请求问题,您应该将当前问题存储在您的房间中并发送 currentQuestion,而不是生成它。

I think your room should keep some state, like game started, waiting for clients etc... Clients all send a ready, once all clients are ready room generates a question and sends it to all clients.我认为您的房间应该保持某种状态,例如游戏开始,等待客户等...客户都发送就绪,一旦所有客户都准备好,房间就会生成一个问题并将其发送给所有客户。 Something like so.像这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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