[代码]var[代码] [代码]me = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]this[代码][代码].socket = {[代码][代码] [代码][代码]onopen: [代码][代码]this[代码][代码]._onOpen,[代码][代码] [代码][代码]onmessage: [代码][代码]this[代码][代码]._connect_cb_wrapper,[代码][代码] [代码][代码]onerror: [代码][代码]this[代码][代码]._onError,[代码][代码] [代码][代码]onclose: [代码][代码]this[代码][代码]._onClose,[代码][代码] [代码][代码]send: [代码][代码]function[代码] [代码](str) {[代码][代码] [代码][代码]wx.sendSocketMessage({ data: str });[代码][代码] [代码][代码]},[代码][代码] [代码][代码]};[代码] [代码] [代码][代码]function[代码] [代码]creatSocket() {[代码][代码] [代码][代码]if[代码] [代码](_socketTask) {[代码][代码] [代码][代码]console.log([代码][代码]'有sockettask了。。。'[代码][代码])[代码][代码] [代码][代码]_socketTask.close();[代码][代码] [代码][代码]_socketTask = undefined;[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]setTimeout(() => {[代码][代码] [代码][代码]var[代码] [代码]SocketTask = wx.connectSocket({[代码][代码] [代码][代码]url: me._conn.service, [代码][代码] [代码][代码]protocols: [[代码][代码]'xmpp'[代码][代码]][代码][代码] [代码][代码]// fail: function (e) {[代码][代码] [代码][代码]// console.log('连接失败', e)[代码][代码] [代码][代码]// me._conn._changeConnectStatus(Strophe.Status.CONNFAIL, null);[代码][代码] [代码][代码]// //部分机型从后台切回前台状态有延迟[代码][代码] [代码][代码]// if (e.errMsg.indexOf('suspend') != -1) {[代码][代码] [代码][代码]// creatSocket()[代码][代码] [代码][代码]// }[代码][代码] [代码][代码]// },[代码][代码] [代码][代码]// success: function (e) { [代码][代码] [代码][代码]// console.log('连接成功', e)[代码][代码] [代码][代码]// }[代码][代码] [代码][代码]});[代码][代码] [代码][代码]_socketTask = SocketTask;[代码] [代码] [代码][代码]_socketTask.onOpen([代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log([代码][代码]"WebSocket 连接已打开!"[代码][代码]);[代码][代码] [代码][代码]isSocketConnnected = [代码][代码]true[代码][代码] [代码][代码]// wx.sendSocketMessage({[代码][代码] [代码][代码]// data: JSON.stringify({ id: 'csq@10.4.0.66' }),[代码][代码] [代码][代码]// })[代码][代码] [代码][代码]me.socket.onopen.call(me); [代码][代码] [代码][代码]me._conn._changeConnectStatus(Strophe.Status.CONNECTED, [代码][代码]null[代码][代码]);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]_socketTask.onMessage([代码][代码]function[代码] [代码](msg) {[代码][代码] [代码][代码]//console.log("onSocketMessage", msg, JSON.stringify(msg));[代码][代码] [代码][代码]me.socket.onmessage.call(me, msg);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]_socketTask.onClose([代码][代码]function[代码] [代码](e) {[代码][代码] [代码][代码]console.log([代码][代码]"WebSocket 连接已经关闭!"[代码][代码],e);[代码][代码] [代码][代码]isSocketConnnected = [代码][代码]false[代码][代码] [代码][代码]me._conn.connected = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]me.socket.onclose.call(me);[代码][代码] [代码][代码]// 外部回调,需要设计一个更合适的[代码][代码] [代码][代码]me._onSocketClose && me._onSocketClose(e);[代码][代码] [代码][代码]me._conn._changeConnectStatus(Strophe.Status.DISCONNECTED, e);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]_socketTask.onError([代码][代码]function[代码] [代码](e) {[代码][代码] [代码][代码]console.log([代码][代码]'出错了 出错了'[代码][代码])[代码][代码] [代码][代码]me.socket.onclose.call(me); [代码][代码] [代码][代码]})[代码] [代码] [代码][代码]}, 1000)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]creatSocket();[代码] 参照了一下环信的api。[图片] 正常情况下,会有5个消息。[图片] 现在只有一个。
xmpp类库Strophe?我使用了Strophe.js库,更改了里面的new Websocket为wx的websocket相关动作,本以为更换了套接字就ok,没想到strophe.js只能正常链接,缺没法登录验证,求解
2019-10-31