123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- // socket已经连接成功
- var socketOpen = false
- // socket已经调用关闭function
- var socketClose = false
- // socket发送的消息队列
- var socketMsgQueue = []
- // 判断心跳变量
- var heart = ''
- // 心跳失败次数
- var heartBeatFailCount = 0
- // 终止心跳
- var heartBeatTimeOut = null;
- // 终止重新连接
- var connectSocketTimeOut = null;
- var webSocket = {
- /**
- * 创建一个 WebSocket 连接
- * @param {options}
- * url String 是 开发者服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名
- * header Object 否 HTTP Header , header 中不能设置 Referer
- * method String 否 默认是GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- * protocols StringArray 否 子协议数组 1.4.0
- * success Function 否 接口调用成功的回调函数
- * fail Function 否 接口调用失败的回调函数
- * complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
- */
- connectSocket: function (options) {
- socketOpen = false
- socketClose = false
- socketMsgQueue = []
- wx.connectSocket({
- url: 'wss://xiaochengxuapi.jrjyjrjy.com/ws',
- success: function (res) {
- if (options) {
- // 成功回调
- options.success && options.success(res);
- }
- },
- fail: function (res) {
- if (options) {
- // 失败回调
- options.fail && options.fail(res);
- }
- }
- })
- },
- /**
- * 通过 WebSocket 连接发送数据
- * @param {options}
- * data String / ArrayBuffer 是 需要发送的内容
- * success Function 否 接口调用成功的回调函数
- * fail Function 否 接口调用失败的回调函数
- * complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
- */
- sendSocketMessage: function (options) {
- if (socketOpen) {
- wx.sendSocketMessage({
- data: options.msg,
- success: function (res) {
- if (options) {
- options.success && options.success(res);
- }
- },
- fail: function (res) {
- console.log(res);
- if (options) {
- options.fail && options.fail(res);
- }
- }
- })
- } else {
- socketMsgQueue.push(options.msg)
- }
- },
- /**
- * 关闭 WebSocket 连接。
- * @param {options}
- * code Number 否 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。如果这个参数没有被指定,默认的取值是1000 (表示正常连接关闭)
- * reason String 否 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于123字节的UTF-8 文本(不是字符)
- * fail Function 否 接口调用失败的回调函数
- * complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
- */
- closeSocket: function (options) {
- if (connectSocketTimeOut) {
- clearTimeout(connectSocketTimeOut);
- connectSocketTimeOut = null;
- }
- socketClose = true;
- var self = this;
- self.stopHeartBeat();
- wx.closeSocket({
- success: function (res) {
- console.log('WebSocket 已关闭!');
- if (options) {
- options.success && options.success(res);
- }
- },
- fail: function (res) {
- if (options) {
- options.fail && options.fail(res);
- }
- }
- })
- },
- // 收到消息回调
- onSocketMessageCallback: function (msg) {
- },
- // 开始心跳
- startHeartBeat: function () {
- console.log('socket开始心跳')
- var self = this;
- heart = 'heart';
- self.heartBeat();
- },
- // 结束心跳
- stopHeartBeat: function () {
- console.log('socket结束心跳')
- var self = this;
- heart = '';
- if (heartBeatTimeOut) {
- clearTimeout(heartBeatTimeOut);
- heartBeatTimeOut = null;
- }
- if (connectSocketTimeOut) {
- clearTimeout(connectSocketTimeOut);
- connectSocketTimeOut = null;
- }
- },
- // 心跳
- heartBeat: function () {
- var self = this;
- if (!heart) {
- return;
- }
- self.sendSocketMessage({
- msg: JSON.stringify({
- form_id: getApp().UserId(),
- type: 'resetChatTotal'})})
- self.sendSocketMessage({
- msg: JSON.stringify({
- form_id: getApp().UserId(),
- type: 'login'
- }),
- success: function (res) {
- console.log('socket心跳成功');
- if (heart) {
- heartBeatTimeOut = setTimeout(() => {
- self.heartBeat();
- }, 120000);
- }
- },
- fail: function (res) {
- console.log('socket心跳失败');
- if (heartBeatFailCount > 2) {
- // 重连
- self.connectSocket();
- }
- if (heart) {
- heartBeatTimeOut = setTimeout(() => {
- self.heartBeat();
- }, 7000);
- }
- heartBeatFailCount++;
- },
- });
- }
- }
- // 监听WebSocket连接打开事件。callback 回调函数
- wx.onSocketOpen(function (res) {
- console.log('WebSocket连接已打开!')
- // wx.hideLoading();
- // 如果已经调用过关闭function
- if (socketClose) {
- webSocket.closeSocket();
- } else {
- socketOpen = true
- // webSocket.sendSocketMessage({
- // msg: JSON.stringify({
- // form_id: getApp().UserId(),
- // type: 'login'
- // }),
- // success: function (e) {
- // console.log('socket登录成功');
- // }
- // })
- for (var i = 0; i < socketMsgQueue.length; i++) {
- webSocket.sendSocketMessage(socketMsgQueue[i])
- }
- socketMsgQueue = []
- webSocket.startHeartBeat();
- }
- })
- // 监听WebSocket错误。
- wx.onSocketError(function (res) {
- console.log('WebSocket连接打开失败,请检查!', res)
- })
- // 监听WebSocket接受到服务器的消息事件。
- wx.onSocketMessage(function (res) {
-
- let data = JSON.parse(res.data);
- console.log(data);
- switch(data.type){
- case 'resetChatTotal':
- if (data.data.message_exists == true){
- wx.showTabBarRedDot({
- index: 0,
- })
- }else{
- wx.removeTabBarBadge({
- index: 0,
- });
- // wx.setTabBarBadge({
- // index: 0
- // })
- }
- break;
- }
- console.log('没有回调之前的方法:' + res.data)
- webSocket.onSocketMessageCallback(res.data);
- })
- // 监听WebSocket关闭。
- wx.onSocketClose(function (res) {
- console.log('WebSocket 已关闭!')
- if (!socketClose) {
- clearTimeout(connectSocketTimeOut)
- connectSocketTimeOut = setTimeout(() => {
- webSocket.connectSocket();
- }, 3000);
- }
- })
- // 监听WebSocket关闭。
- wx.onSocketClose(function (res) {
- console.log('WebSocket 已关闭!')
- if (!socketClose) {
- clearTimeout(connectSocketTimeOut)
- connectSocketTimeOut = setTimeout(() => {
- webSocket.connectSocket();
- }, 3000);
- }
- })
- module.exports = webSocket;
|