gameSocket.ts 4.5 KB

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