最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

多次触发NodeJS的套接字IO事件

网站源码admin21浏览0评论

多次触发NodeJS的套接字IO事件

多次触发NodeJS的套接字IO事件

我正在使用socket.IO在NodeJS和Android中构建一个测验应用程序,

当我从服务器发出事件quizzoStatus时,我遇到问题,事件第一次触发一次,第二次触发两次,依此类推。

在这里,我附上了我的代码片段

///server side: NodeJS
socket.on('sendQuizzoAnsPoints', async (data)=>{
          try {
              const obj = JSON.parse(data);
              const game = await QuizzoPlayModel.findOne({_id:obj.gameId});
              const player = await UserModel.findOne({_id: game.playerId});
              const opponent = await UserModel.findOne({_id: game.opponentId});
              if(obj.userId == game.opponentId){
                  let update = {
                      opponentPoints: game.opponentPoints + obj.points || 0,
                      opponentWA: game.opponentWA + obj.wrongAns || 0,
                  };
                  await QuizzoPlayModel.findByIdAndUpdate(obj.gameId, update).lean().exec();
                  userNamespace.to(player.socketId).emit('quizzoStatus', {
                      fullName: opponent.fullName,
                      points: game.playerPoints + obj.points,
                      wrongAns: obj.wrongAns,
                      gameId: obj.gameId
                  });
              }
              if(obj.userId == game.playerId) {
                  let update = {
                      playerPoints: game.playerPoints + obj.points || 0,
                      playerWA: game.playerWA + obj.wrongAns || 0,
                  };
                  await QuizzoPlayModel.findByIdAndUpdate(obj.gameId, update).lean().exec();
                  userNamespace.to(opponent.socketId).emit('quizzoStatus', {
                      fullName: player.fullName,
                      points: game.playerPoints+ obj.points,
                      wrongAns: obj.wrongAns,
                      gameId: obj.gameId
                  });
              }
          } catch (e) {
              console.log(e);
          }
        });

在这里,我听一个名为sendQuizzoAnsPoints的事件,然后在另一个名为quizzoStatus的事件中向玩家或对手发出一个事件。

quizzoStatus事件从服务器到android多次触发。这里我附上了android代码

/// Android code
socket.emit("sendQuizzoAnsPoints", new Gson().toJson(quizzoStatusRequestDto)); 
socket.on("quizzoStatus", new Emitter.Listener(){
           @Override
           public void call(Object... args){
               runOnUiThread(new Runnable(){
                   @Override
                   public void run(){
                       Log.e("opponet point", opponentPoints + " " + quizzoStatusResponseDto.getPoints());
                   }
               });
           }
       });
回答如下:

问题出在Android。您每次都要分配新的侦听器而不删除它。您需要创建该Emmiter侦听器的变量,并在完成工作后将其删除onDestroy或其他位置:

    //variable of Emmiter.Listener
    Emmiter.Listener quizzoStatus = new Emitter.Listener(){
        @Override public void call(Object... args){
            runOnUiThread(new Runnable(){
                @Override public void run(){
                    Log.e("opponet point", opponentPoints + " " + quizzoStatusResponseDto.getPoints());
                }
            });
        }
    };

    //assigning the listener
    socket.on("quizzoStatus", quizzoStatus);
    . . . .
    @Override protected void onDestroy(){
        super.onDestroy();
        //removing the listener...
        socket.off("quizzoStatus", quizzoStatus);
    }

希望这会奏效

发布评论

评论列表(0)

  1. 暂无评论