QuickBlox 的 QB.chat.addlistener 仅偶尔工作,无法说明原因或触发它的原因

Posted

技术标签:

【中文标题】QuickBlox 的 QB.chat.addlistener 仅偶尔工作,无法说明原因或触发它的原因【英文标题】:QuickBlox' QB.chat.addlistener only working sporadically, can't tell why or what triggers it 【发布时间】:2015-06-05 19:29:50 【问题描述】:

在看似随机的时间难以完成这项工作。当我进入聊天页面时,控制器会激活,然后立即触发前 addlistener 和后 addlistener console.logs,然后有时当我发送消息时,会收到包含包内容的控制台打印输出的消息,但更多通常不是。有时,它会打印在触发发送消息而不是接收端的计算机控制台上。

不过,至少,每当我发送消息时,即使我没有得到包的控制台打印输出,我也会得到相应的 [QBChat RECV]:, [object Object] - 按十次按钮,获得十个 QBChat RECV。

.controller('ChatCtrl', function($scope, $stateParams, $timeout, $rootScope, $ionicLoading) 
  console.log("Inside ChatCtrl");
  
  QB.createSession(function(err,result)
    console.log('Session create callback', err, result);
    console.log('Session create callback' + JSON.stringify(result));
  );

  $scope.settings = ;
  $scope.user = ;
  $scope.error = ;

  $scope.signInClick = function() 
    console.log('Login was clicked');
     
      $scope.loading = $ionicLoading.show(
          content: 'Logging in',
          animation: 'fade-in',
          showBackdrop: true,
          maxWidth: 200,
          showDelay: 0
      );
      var params = 'login': ($scope.user.username), 'password': ($scope.user.password)
      console.log("params... " + JSON.stringify(params));

      QB.users.create(params, function(err, user)
        if (user) 
          console.log("successful user.create... " + JSON.stringify(user));
          var jid = user.id + "-#####" + "@chat.quickblox.com";
          var chatparams = 'jid': jid, 'password': ($scope.user.password);
          QB.chat.connect(chatparams, function(err, roster) 
            console.log("err from qb.chat.connect... " + JSON.stringify(err));
            console.log("roster from qb.chat.connect .... " + JSON.stringify(roster));
          );
         else  
          if (err.message == "Unprocessable Entity")
            QB.login(params, function(err, user)
              if (user) 
                console.log("Logged into QB with " + JSON.stringify(user));
                var jid = user.id + "-#####" + "@chat.quickblox.com";
                console.log(user.login + "'s jid is......" + jid);
                var chatparams = 'jid': jid, 'password': ($scope.user.password);
                QB.chat.connect(chatparams, function(err, roster) 
                  console.log("stringifying the roster... " + JSON.stringify(roster));
                );
               
              else  
                console.log(JSON.stringify(err));
              
            );
          
        
      );
      
      // var chatparams = 'jid': jid, 'password': ($scope.user.password);
      // console.log("the jid is......" + jid);
      // console.log("chatparams is ......" + JSON.stringify)

      Parse.User.logIn(($scope.user.username) , $scope.user.password, 
          success: function(_user) 
              console.log('Login Success');
              console.log('user = ' + _user.attributes.username);
              console.log('email = ' + _user.attributes.email);
              $ionicLoading.hide();
              $rootScope.user = _user;
              $rootScope.isLoggedIn = true;
              // $state.go('tab.home');
          ,
          error: function(user, err) 
              $ionicLoading.hide();
              // The login failed. Check error to see why.
              if (err.code === 101) 
                  $scope.error.message = 'Invalid login credentials';
               else 
                  $scope.error.message = 'An unexpected error has ' +
                      'occurred, please try again.';
              
              $scope.$apply();
          
      );
    // $state.go('tab.profile');
  ;


  $scope.sendMessageClick = function() 

    var user = $rootScope.user.attributes.username;
    console.log("user = " + user);
    console.log('sendMessageclick');
    
    var countchew = "3354163-#####@chat.quickblox.com"; //countchew
    var starshipenterprise = "3354099-#####@chat.quickblox.com"; //starshipenterprise
    QB.chat.roster.get(function(roster) 
      console.log("roster.get before if block  " + JSON.stringify(roster));
    );
    if (user == "countchew")
      QB.chat.roster.confirm(starshipenterprise, function()
        console.log("roster.confirm called");
      );
      QB.chat.roster.add(starshipenterprise, function() 
        console.log("roster.add called");
      );
      QB.chat.send(starshipenterprise, 
          type: 'chat',
          name: 'testmessage',
          body: 'Hello world!',
          extension: save_to_history: 1
      );
      // QB.chat.roster.remove(starshipenterprise, function() 
      //   console.log("roster.remove starship ... ");
      // );
      QB.chat.roster.get(function(roster) 
        console.log("end of if statement  " + JSON.stringify(roster));
      );
     else if (user == "starshipenterprise")
      QB.chat.roster.confirm(countchew, function() 
        console.log("roster.confirm called");
      );
      QB.chat.roster.add(countchew, function() 
        console.log("roster.add called");
      );
      QB.chat.send(countchew, 
          type: 'chat',
          body: 'Hello world!'
      );
    

  ;
  
  console.log("before addlistener");
  QB.chat.addListener(from: '3354163-#####@chat.quickblox.com', function() 
    QB.chat.onMessageListener = function(userId, message) 
      console.log('userId ..... ' + userId);
      console.log('message .... ' + JSON.stringify(message));
    ;
  );
  console.log("after addlistener");


  var chatparams1 = from: '3354099-#####@chat.quickblox.com';
  console.log("before addlistener");
  QB.chat.addListener(chatparams1, function() 
    QB.chat.onMessageListener = function(userId, message) 
      console.log('userId ..... ' + userId);
      console.log('message .... ' + JSON.stringify(message));
    ;
  );
  console.log("after addlistener");

  )

【问题讨论】:

【参考方案1】:

太棒了!想通了。

您需要注意它发送的 XML 包。发生的事情是,不知何故,当它首次启动并运行并与该帐户的相应 QB 聊天地址正常工作时,之后,在某个时候它开始附加一串数字-quickblox-numbers (35896363-quickblox- 20942 或其他)在地址的“发件人”字段中,这是我让我的听众听到的。令人困惑的是,这偶尔会起作用。

您不能对地址进行硬编码,因为末尾的这串数字会随着每次登录而改变。

相反,它暂时只监听带有参数 name: 'message', type: 'chat' 的消息。

【讨论】:

【参考方案2】:

更好的方法可能是:

QB.chat.addListener( name: 'message', function() 
            $log.debug(" main: QB listener by filter was triggered");
);

或者只是:

QB.chat.onMessageListener = showMessage;

该代码必须运行一次(在第一次初始化时,在系统加载时)。

onMessageListener 每次都会被触发。 addListener 仅在 msg 名称为“message”时才会触发。 (监听器的过滤器)

那么你需要一个带有这样签名的函数:

function showMessage(userId, msg) 
    console.log('main.showMessage:: on message', msg)

【讨论】:

以上是关于QuickBlox 的 QB.chat.addlistener 仅偶尔工作,无法说明原因或触发它的原因的主要内容,如果未能解决你的问题,请参考以下文章

QBRoomChatManager 类在以后的 Quickblox 库中不存在 - quickblox-sdk-release-2

quickblox 中的门控错误(“未定义 quickblox unreadMessageCount”)

没有收到推送通知,quickblox,android

限制 quickblox 用户创建

在 IOS 中使用 QuickBlox 视频聊天,无需登录或密码

Quickblox 膨胀的 webpack 包