vLine:recv:im触发两次

时间:2013-07-15 15:49:35

标签: webrtc vline

我已经开始将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 app文件内容:

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

我必须指定聊天对话是在两个不同的用户之间,其中包含来自Vline的2个不同的令牌。

有什么建议吗?

1 个答案:

答案 0 :(得分: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);
}