簡體   English   中英

從客戶端向所有房間發出功能一次,socket.io

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

我是 socket.io 的新手,我很難弄清楚如何正確使用服務器和客戶端。 所以,我在客戶端有一個函數,它監聽服務器以獲取一系列問題以將其顯示到房間。

這個想法是創建一個連接了多個客戶端的房間,並從客戶端向一個房間發出功能(僅一次,因為我想向所有房間顯示信息)。 我真的需要在我的客戶端使用這個功能,因為我有大約 100 行涉及前端的代碼。 我的服務器還需要將其數組發送給客戶端。 我的問題是,因為它是客戶端,所以當我將它發送到服務器時,它會被發送 x 倍於房間內的客戶端數量。 我希望只發射一次。

舉一個具體的例子:我的服務器有一個數組對象,它被傳遞給房間中的客戶端,以便他們可以顯示它。 這是客戶端中的功能:

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

然后我在我的服務器中發出它以獲取問題對象

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

而且我正在向我的客戶發出回音(我不確定這是否是好的做法,但我不知道如何收聽該對象)

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

這段代碼的問題是:因為它是從客戶端調用的,如果房間里有兩個客戶端,它會被調用兩次,如果有三個客戶端,它會被調用三次,所以當我開始游戲時它會通過從問題 1 到問題 3(如果客戶數量是三個)。 我希望我說的有道理,如果沒有,請毫不猶豫地告訴我

有誰知道我該如何糾正它?

您遇到的主要問題是服務器行為是由客戶端驅動的,這應該是相反的......

為什么您的客戶要提出新問題? 您的服務器應在可用時發送新問題。 如果您確實需要客戶請求問題,您應該將當前問題存儲在您的房間中並發送 currentQuestion,而不是生成它。

我認為您的房間應該保持某種狀態,例如游戲開始,等待客戶等...客戶都發送就緒,一旦所有客戶都准備好,房間就會生成一個問題並將其發送給所有客戶。 像這樣的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM