import utils from "../Network/netUtils";
export default class GameNet{
    private static instance: GameNet = null;
    private ws: WebSocket = null;
 
    private constructor(){}
 
    public static getInstance(): GameNet{
        if(GameNet.instance === null){
            GameNet.instance = new GameNet();
        }
        return GameNet.instance;
    }
 
    public init(id, callback: Function, target: any){
        if(this.ws != null)return;
        // https://www.yuyekeji.cn/api_dapp/index 192.168.0.106:26001 
        // wss://www.yuyekeji.cn/api_dapp/websocket/dappBack/
        console.log("utils.token:",utils.token);
        this.ws = new WebSocket(window['dappWss']+id,["11"]);
        this.ws.onopen = (event: Event)=>{
            callback.call(target, "登录成功");
        }
 
        this.ws.onmessage = (event: MessageEvent)=>{
            callback.call(target, event.data);
        }

        this.ws.onerror = function (e) {
            console.error("WebSocket连接发生错误");
        };
    }
 
    public sendMsg(data: string){
        if(this.ws != null && this.ws.readyState != WebSocket.OPEN)return;
        this.ws.send(data);
    }
}