gameSocket.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { _decorator, Component, Node, Game, sys } from 'cc';
  2. import { Constant } from '../constant';
  3. import { GameMng } from '../GameMng';
  4. import GameUtil from '../gcommon/GameUtil';
  5. import { Tools } from '../Tools';
  6. import { msgManager } from './msgManager';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('gameSocket')
  9. export default class gameSocket {
  10. private static _instance: gameSocket | null = null;
  11. public static get Instance() {
  12. if (gameSocket._instance === null)
  13. gameSocket._instance = new gameSocket();
  14. return gameSocket._instance;
  15. }
  16. sock:WebSocket = null;
  17. is_connected:Boolean = false;
  18. stopConnected:Boolean = false;
  19. check_heartbeat_succeed:boolean = false;
  20. is_online_status:boolean = false; //是否是在线状态
  21. _on_opened(event:Event):void
  22. {
  23. if(this.is_connected===true){
  24. }else{
  25. this.is_connected = true;
  26. this.check_heartbeat_succeed = true;
  27. gameSocket.Instance.is_online_status = true;
  28. if(GameMng._userData!=null){
  29. msgManager.relink()
  30. }else{
  31. }
  32. }
  33. console.log("_on_opened",event)
  34. }
  35. _on_recv_data(event:any):void
  36. {
  37. this.check_heartbeat_succeed = true;
  38. if(event.data===Constant.RECV_HEART_BEAT){
  39. //console.log("接受服务端返回的心跳",GameUtil.curTime())
  40. return
  41. }
  42. if(event.data===Constant.RECV_CHEAK_HEART_BEAT){
  43. gameSocket.Instance.heartbeat();
  44. return
  45. }
  46. if(event.data===""||event.data===null){
  47. console.assert(true,"接受输出格式错误,data为空",event)
  48. }else{
  49. msgManager.recv(event.data)
  50. }
  51. }
  52. _on_socket_close(event:any):void
  53. {
  54. this.is_connected = false;
  55. console.log("_on_socket_close",event)
  56. }
  57. _on_socket_err(event:any):void
  58. {
  59. this.is_connected = false;
  60. // this.close()
  61. console.log("_on_socket_err",event)
  62. }
  63. connect(url:any):void
  64. {
  65. if(this.stopConnected){
  66. console.log("客户端退出链接,暂不提供重连")
  67. return
  68. }
  69. if(!this.is_connected){
  70. try{
  71. this.sock = new WebSocket(url);
  72. this.sock.binaryType = "arraybuffer"; //"arraybuffer", "blob", "document", "json", and "text".
  73. this.sock.onopen = this._on_opened.bind(this);
  74. this.sock.onmessage = this._on_recv_data.bind(this);
  75. this.sock.onclose = this._on_socket_close.bind(this);
  76. this.sock.onerror = this._on_socket_err.bind(this);
  77. }catch(e:any){
  78. console.log("connect error",e)
  79. }
  80. }
  81. }
  82. heartbeat(){
  83. this.check_heartbeat_succeed = false;
  84. this.send_cmd(0);
  85. }
  86. // this.sock.readyState
  87. // CONNECTING:值为0,表示正在连接;
  88. // OPEN:值为1,表示连接成功,可以通信了;
  89. // CLOSING:值为2,表示连接正在关闭;
  90. // CLOSED:值为3,表示连接已经关闭,或者打开连接失败。
  91. send_cmd(body):void
  92. {
  93. if (!this.sock || !this.is_connected) {
  94. return;
  95. }
  96. console.log("发送消息",body)
  97. try{
  98. if( this.sock.readyState===1){
  99. if( typeof body == 'number'){
  100. body=body+""
  101. }
  102. this.sock.send(body);
  103. }
  104. // if(sys.isBrowser){
  105. // }else{
  106. // this.sock.send(body);
  107. // }
  108. }catch(e:any){
  109. console.log("e",e)
  110. }
  111. }
  112. close():void
  113. {
  114. if (this.sock !== null) {
  115. this.sock.close();
  116. this.sock = null;
  117. }
  118. this.is_connected = false;
  119. this.check_heartbeat_succeed = false;
  120. }
  121. }