
import WebsocketHeartbeat from "./WebsocketHeartbeat";

var server = "";  
var aServer="";
if (process.env.NODE_ENV === 'development') {
    server = "ws://localhost:7018/imserver/r:";
    aServer = "ws://localhost:7018/imserver/u:";
}else{
    server = "wss://im.yjf.runtzh.com/imserver/r:";  
    aServer = "wss://im.yjf.runtzh.com/imserver/u:";  
    //server = "ws://139.186.77.83:7018/imserver/r:";  
}

export default {
    socket: {},
    aSoket:{},
    hasLogin:false,
    initSocket: function(uid,reset) {
        if(typeof(WebSocket) === "undefined"){
            console.log("您的浏览器不支持socket")
        }else{
            // 实例化socket
            if(!this.socket || reset){
                console.log("实例化socket");
                //this.socket = new WebSocket(server+uid);
                this.socket=new WebsocketHeartbeat({
                    url: server+uid,
                    userId:"r:"+uid,
                    pingTimeout: 15000,
                    pongTimeout: 10000
                });
                // this.socket.onopen = function () {
                //     console.log('connect success');
                //     console.log('send massage: test');
                //     websocketHeartbeatJs.send('test');
                //     setTimeout(() => {
                //         console.log(`wait ${websocketHeartbeatJs.opts.pingTimeout} ms will hava '${websocketHeartbeatJs.opts.pingMsg}'`);
                //     }, 1500);
                // }

                this.hasLogin=true;
            }
            
        }
    },
    initASocket: function(uid,reset,that) {
        if(typeof(WebSocket) === "undefined"){
            that.$notify.error({
                title: '您的浏览器不支持socket',
                showClose: false
            });
            console.log("您的浏览器不支持socket")
        }else{
            // 实例化socket
            if(!this.aSoket || reset){
                console.log("实例化aSocket");
                this.aSoket=new WebsocketHeartbeat({
                    url: aServer+uid,
                    userId:"u:"+uid,
                    pingTimeout: 15000,
                    pongTimeout: 10000
                });
            }
            
        }
    }
}