小程序集成環信IM

最近在作一款有語音直播功能的小程序,用到了環信IM集成功能,因爲我搜了下目前用環信作小程序的的確是少之又少,並且環信官方說從2月份再也不更新代碼(具體緣由我也沒問,可能自己微信就是一款聊天工具因此用處不大)ajax

咱們產品須要用到聊天室功能,以前作H5端登陸完成以後加入聊天室便可:小程序

WebIM.conn.open(options);
 WebIM.conn.joinChatRoom(option1);

一樣方法在小程序上不行,一直報錯:api

Cannot read property 'sendIQ' of undefined

百度了一下,說加入聊天室要放在登陸成功後的回調裏,因而查看它的源碼connection.js發現:微信

connection.prototype.open = function (options) {


    var pass = _validCheck(options, this);

    if (!pass) {
        return;
    }
    var conn = this;

    if (conn.isOpening() || conn.isOpened()) {
        return;
    }

    if (options.accessToken) {
        options.access_token = options.accessToken;
        _login(options, conn);
    } else {
       //登陸成功的回調函數
        var suc = function (data, xhr, myName) {
            conn.context.status = _code.STATUS_DOLOGIN_IM;
            conn.context.restTokenData = data;
            if (data.statusCode != '404' && data.statusCode != '400') {
                wx.showToast({
                    title: '登陸成功',
                    icon: 'none',
                    duration: 4000
                });
            }
            //回調成功後執行這個方法
            _login(data.data, conn);
           
        };
        
        var options = {
            url: apiUrl + '/' + orgName + '/' + appName + '/token',
            data: loginfo,
             success: suc || _utils.emptyfn,
            error: error || _utils.emptyfn
        };
        _utils.ajax(options);
    }

登陸成功有一個內部回調,回調裏面調用了一個 _login(data.data, conn)的方法:app

//具體裏面執行什麼去源代碼查看,我就不貼代碼了
var _login = function (options, conn) {
    
    var callback = function (status, msg) {
      _loginCallback(status, msg, conn);
    };


};

執行完login方法有一個_loginCallback回調:函數

var _loginCallback = function (status, msg, conn) {
    var conflict, error;
    //console.log('_loginCallback 1', Strophe.Status, status, msg)
    if (msg === 'conflict') {
        conflict = true;
    }
    console.log(status)
    if (status == Strophe.Status.CONNFAIL) {
     
    } else if (status == Strophe.Status.ATTACHED || status == Strophe.Status.CONNECTED) {
      //登陸成功
       
    } else if (status == Strophe.Status.DISCONNECTING) {
  
    } else if (status == Strophe.Status.DISCONNECTED) {
      
    } else if (status == Strophe.Status.AUTHFAIL) {
     
    } else if (status == Strophe.Status.ERROR) {

    }
    conn.context.status_now = status;
};

經過斷點發現當執行到 else if (status == Strophe.Status.ATTACHED || status == Strophe.Status.CONNECTED)時,登陸成功.因此須要把加入聊天室的回調須要放在這個判斷裏面,個人具體作法以下:工具

//項目中封裝一個加入聊天室函數
 joinRoom: function () {
    var option1 = {};
    var option2 = {};
    //加入聊天室
    WebIM.conn.joinChatRoom(option1);
    WebIM.conn.joinChatRoom(option2);
  };
   //在環信登陸函數中添加一個本身的回調:
    var options = {
      apiUrl: WebIM.config.apiURL,
      user: userId + '',
      pwd: userId + '',
      grant_type: "password",
      appKey: WebIM.config.appkey,
      //本身添加的回調函數
      callBack:function(){
      //調用加入聊天室
        that.joinRoom();
      }
    };
    WebIM.conn.open(options);

修改環信connection.js代碼:this

//登陸代碼
connection.prototype.open = function (options) {

        var suc = function (data, xhr, myName) {
            //callBack傳入加入聊天室回調
            _login(data.data, conn, options.callBack);
        };
        var options = {
            url: apiUrl + '/' + orgName + '/' + appName + '/token',
            data: loginfo,
             success: suc || _utils.emptyfn,
             //添加加入聊天室的回調函數
             callBack: options.callBack,
            error: error || _utils.emptyfn
        };
    }
};
//_login代碼
var _login = function (options, conn, callBack) {
     
    var callback = function (status, msg) {
    //把加入聊天室函數傳給_login回調函數
      _loginCallback(status, msg, conn, callBack);
    };
};
//_loginCallback代碼
var _loginCallback = function (status, msg, conn,callBack) {
    var conflict, error;
    //console.log('_loginCallback 1', Strophe.Status, status, msg)
    if (msg === 'conflict') {
        conflict = true;
    }
    console.log(status)
    if (status == Strophe.Status.CONNFAIL) {
     
    } else if (status == Strophe.Status.ATTACHED || status == Strophe.Status.CONNECTED) {
      //登陸成功執行加入聊天室
       callBack();
    } else if (status == Strophe.Status.DISCONNECTING) {
  
    } else if (status == Strophe.Status.DISCONNECTED) {
      
    } else if (status == Strophe.Status.AUTHFAIL) {
     
    } else if (status == Strophe.Status.ERROR) {

    }
    conn.context.status_now = status;
};

具體思路就是這樣,因爲環信小程序代碼有不少坑,因此遇到問題多看看源碼,有些功能就須要本身去修改代碼,這是環信工單回答個人問題:url

clipboard.png

相關文章
相關標籤/搜索