GameNet.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import utils from "../Network/netUtils";
  2. export default class GameNet{
  3. private static instance: GameNet = null;
  4. private ws: WebSocket = null;
  5. private constructor(){}
  6. public static getInstance(): GameNet{
  7. if(GameNet.instance === null){
  8. GameNet.instance = new GameNet();
  9. }
  10. return GameNet.instance;
  11. }
  12. public init(id, callback: Function, target: any){
  13. if(this.ws != null)return;
  14. // https://www.yuyekeji.cn/api_dapp/index 192.168.0.106:26001
  15. // wss://www.yuyekeji.cn/api_dapp/websocket/dappBack/
  16. console.log("utils.token:",utils.token);
  17. this.ws = new WebSocket(window['dappWss']+id,["11"]);
  18. this.ws.onopen = (event: Event)=>{
  19. callback.call(target, "登录成功");
  20. }
  21. this.ws.onmessage = (event: MessageEvent)=>{
  22. callback.call(target, event.data);
  23. }
  24. this.ws.onerror = function (e) {
  25. console.error("WebSocket连接发生错误");
  26. };
  27. }
  28. public sendMsg(data: string){
  29. if(this.ws != null && this.ws.readyState != WebSocket.OPEN)return;
  30. this.ws.send(data);
  31. }
  32. }