
import website from "@/config/website";
import { getStore} from "@/util/store";
import { getUserInfo} from '@/api/user'
const userInfo=getStore({name: 'userInfo'}) || [];
let ws = {};
let socket
ws.msgCount={
    myMessageCount:'0',
    userId:'1111111'
}
ws.setInitSocket= function(uid){
	const user_id = userInfo.user_id;
        socket = new WebSocket(website.socket + user_id);
        socket.onopen = function (evt) {
            console.log('websocket链接成功');
        };
      
        socket.onclose = function (evt) {
            console.log('websocket连接已断开')
        };
        let that=this
        socket.onmessage = function ({data}) {
            if (data) {
                if(JSON.parse(data)!==null){
                   ws.msgCount = JSON.parse(data)
                    
                }
            }
            console.log(data,'消息信息')
        };
        socket.onerror = function ({data}) {
            console.log('发生错误:', data)
        };

}
ws.socketSend= function(uid){
	const user_id = userInfo.user_id;
        console.log('发送消息:',)
        if (socket) {
            socket.send(user_id);
        } else {
            setTimeout(()=>{
                socket.send(user_id);
            }, 1000)
        }
    

}
export default ws;
 