0

我已经开始将 vLine API 集成到我的应用程序中,但是遇到了一个问题,recv:im即我发送的每条消息都会发出两次事件。

这是我在模板端发送聊天消息的代码:

$('#chat_room_input').bind('keypress', function(e) {

            if(e.keyCode==13){
                console.log('we send the message here');
                VlineObject.sendMessageToPerson_(remoteUserId);
            }

     });

这也是我的 js 应用程序文件内容:

VlineApp = function(serviceId, current_user, ui_local_widgets, people) {


  this.ui_local_widgets_ = ui_local_widgets;
  this.serviceId_ = serviceId;

  this.current_user_ = current_user;
  this.profile_ = current_user.profile;
  this.auth_token_ = current_user.auth_token;

  this.people_ = people;

  // the only required option is your serviceId 
  this.client_ = vline.Client.create({ serviceId: this.serviceId, "ui": true,  "uiOutgoingDialog":true, "uiIncomingDialog":true, "uiBigGreenArrow":true, "uiVideoPanel": this.ui_local_widgets_.videopanel });

  this.client_.on('login', this.onLoginUpdatePresence_, this);

  // window.PROFILE and window.AUTH_TOKEN are generated by your application server 
  // and set in a script tag in your HTML document 
  this.client_.login(this.serviceId_, this.profile_, this.auth_token_)
      .done(this.init_, this);




}

VlineApp.prototype.init_ = function(session) {

  console.log('here in init ');
  //console.log(this.people_);


  this.session_ = session;
  this.client_.on('recv:im', this.onMessage_, this);

}



VlineApp.prototype.updatePresence = function(e){

  //FUNCTION CALL WHEN THE EVENT IS FIRED
  var person = e.target;
  var presenceState = person.getPresenceState();
  var shortId = person.getShortId();

  this.updatePresenceUI(shortId, presenceState);

}

VlineApp.prototype.updatePresenceUI = function(personid, presenceState) {

  //UPDATE UI PRESENCE STATUS
  $('#'+personid+'_status').html(presenceState);

  /*
   // Show/hide the call link based on presence
   elem = document.getElementById('call-' + shortId);
   if (presenceState === "online" && shortId !== currentUserid) {
   elem.style.display = "";
   } else {
   elem.style.display = "none";
   }
   */

}

VlineApp.prototype.updatePresenceAll = function(person){

  //UPDATE PRESENCE STATUS FOR THE CURRENT PERSON AND ADD TRIGGER EVENT     
  this.updatePresence({target: person});
  person.on('change:presenceState', this.updatePresence, this);

}



VlineApp.prototype.onLoginUpdatePresence_ = function(event){


  this.session_ = event.target;

  for (var i=0; i < this.people_.length; i++)
  {
    this.session_.getPerson(this.people_[i])
        .done(this.updatePresenceAll, this);
  }
}


VlineApp.prototype.showAlertUI = function(sender_name, sender_thumb, message_body) {

  //here we should have push message to the chatroom
  $('#'+this.ui_local_widgets_.chat_room_messages).append('<div>'+sender_name+' :'+message_body+'</div>');
}

VlineApp.prototype.onMessage_ = function(event) {

  console.log('aici in on message');
  var msg = event.message, sender = msg.getSender();
  this.showAlertUI(sender.getDisplayName(), sender.getThumbnailUrl(), msg.getBody());

};

VlineApp.prototype.sendMessageToPersonObj = function(person)
{
  var message = $('#'+this.ui_local_widgets_.chat_room_input).val();
  $('#'+this.ui_local_widgets_.chat_room_input).val('');
  //$('#'+this.ui_local_widgets_.chat_room_messages).append('<div>You :'+message+'</div>');
  this.showAlertUI('You', '', message);
  person.postMessage(message);

}

VlineApp.prototype.sendMessageToPerson_ =  function(personid) {

  if (this.session_)
  {
    this.session_.getPerson(personid)
        .done(this.sendMessageToPersonObj, this);
  }
}

VlineApp.prototype.getMessagesHistoryObj = function(person){

  var messages_history = person.getMessages();
  console.log('messages_history');
  console.log(messages_history);

}

VlineApp.prototype.getMessagesHistory_ = function(personid) {
  console.log(personid);
  console.log(this.session_);
  if (this.session_)
  {
    this.session_.getPerson(personid)
        .done(this.getMessagesHistoryObj, this);
  }
}


VlineApp.prototype.callPersonObj = function(person) {

  person.startMedia();
}

VlineApp.prototype.callPerson_ =  function(personid) {

  if (this.session_)
  {
    this.session_.getPerson(personid)
        .done(this.callPersonObj, this);
  }
}

我必须指定聊天对话是在 2 个不同的用户之间使用 2 个来自 Vline 的不同令牌进行的。

有什么建议么?

4

1 回答 1

1

您似乎不小心登录了两次,并为同一用户获得了两个单独的会话,每个会话都在生成'recv:im'事件。这部分是我们这边的错误(我们不应该让您以同一用户身份登录两次),但您可以解决它。您最终登录两次的原因是我们会自动从本地存储中恢复上一个会话(如果可用)。如果您已经调用过一次并再次点击该页面,它当前会恢复上一个会话,然后在调用client.login()时创建一个新会话。client.login()

要解决此问题,您可以client.isLoggedIn()在调用之前查看您是否已自动登录client.login()

if (this.client_.isLoggedIn()) {
    var session = this.client_.getDefaultSession();
    this.init_(session);
    this.onLoginUpdatePresence_({"target": session});
} else {
    this.client_.on('login', this.onLoginUpdatePresence_, this);

    // window.PROFILE and window.AUTH_TOKEN are generated by your application server 
    // and set in a script tag in your HTML document 
    this.client_.login(this.serviceId_, this.profile_, this.auth_token_)
        .done(this.init_, this);
}
于 2013-07-16T04:06:40.250 回答