socket.js 3.75 KB
var util = require('util.js')
var timer;
function initSocket(){
    connect();
}
function connect(){
    const socketTask= wx.connectSocket({
        url: 'wss://zywm.colorcun.com/wss',
        success: function (res) {
            console.log(res,'xxxxxxx');
        },
        fail: function (fail) {
            console.log(fail,'xxxxxx');
            initSocket();
        }
    });
    // 打开连接
    wx.onSocketOpen(function () {
        console.log('xxxxxxxxxxxx','已经打开')
    })
    wx.onSocketError(function (res) {
        console.log('WebSocket连接打开失败,请检查!')
        wx.onSocketOpen(function () {
            console.log('xxxxxxxxxxxx','已经打开')
        })
    })
    wx.onSocketClose(function(res){
        console.log('WebSocket连接断开,请检查!')
        connect();

    })
    return socketTask;
}

function resiverMessage() {
    console.log('cccccc',timer)
    //接收推送
    var that=this;
    wx.onSocketMessage(function (data) {
        try{
            if(JSON.parse(data.data)){
                console.log('dddddddddd')
                console.log(data.data)
                if(JSON.parse(data.data)['a']=='deleteAuthUser'){
                   if(util.cache('openid')==JSON.parse(data.data)['d']['oi']){
                       wx.showToast({
                           title:'您已被商家移除',
                           icon:'none',
                           duration:2000,
                           success:function () {
                               console.log('xxxxx测试')
                               timer=setInterval(function(){
                                   if(timer){
                                       clearInterval(timer)
                                   }
                                   wx.clearStorageSync();
                                   wx.reLaunch({
                                       url:'/pages/me/s_index',
                                   })
                               },2000)
                               // wx.clearStorageSync();
                               // wx.reLaunch({
                               //     url:'/pages/me/s_index',
                               // })
                           }
                       })
                   }
                }
                if(JSON.parse(data.data)['a']=='transfer'){
                    if(util.cache('openid')==JSON.parse(data.data)['d']['oi']){
                        if(util.cache('sellerid')==JSON.parse(data.data)['d']['os']){
                            wx.showToast({
                                title:'您已被商家移除',
                                icon:'none',
                                duration:2000,
                                success:function () {
                                    timer=setInterval(function(){
                                        if(timer){
                                            clearInterval(timer)
                                        }
                                        wx.clearStorageSync();
                                        wx.reLaunch({
                                            url:'/pages/me/s_index',
                                        })

                                    },2000)
                                    // wx.clearStorageSync();
                                    // wx.reLaunch({
                                    //     url:'/pages/me/s_index',
                                    // })

                                }
                            })
                        }
                        // var pages=getCurrentPages();


                    }
                }
            }
        }catch(e){
            console.log(e);
        }
    })
}
module.exports = {
    initSocket,
    resiverMessage
}