WSManager.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import proto_man from './base/proto_man';
  2. export default class WSManager {
  3. static singleInstance: WSManager = null;
  4. static getInstance(): WSManager {
  5. if (WSManager.singleInstance == null) {
  6. WSManager.singleInstance = new WSManager();
  7. }
  8. return WSManager.singleInstance;
  9. }
  10. sock: WebSocket = null;
  11. serivces_handler: Object = null;
  12. proto_type: number = 0;
  13. is_connected: boolean = false;
  14. // 心跳包ID
  15. heart_beat_id: number = null;
  16. // 心跳检测的间隔时间
  17. heart_beat_time: number = 10;
  18. // 心跳检测的的未连接的次数
  19. heart_beat_num: number = 0;
  20. // 是否已经开启了心跳检测
  21. is_start_heart_beat: boolean = false;
  22. cache_send_cmd: any[] = [];
  23. _on_opened(event) {
  24. console.log("ws connect server success");
  25. this.is_connected = true;
  26. if (this.cache_send_cmd.length > 0) {
  27. let cache = this.cache_send_cmd.shift();
  28. this.send_cmd(cache[0], cache[1], cache[2]);
  29. this.cache_send_cmd = [];
  30. }
  31. }
  32. // 开启心跳检测
  33. _start_check_heart_beat() {
  34. // if (this.is_start_heart_beat) {
  35. // return;
  36. // }
  37. // this.is_start_heart_beat = true;
  38. // this._stop_check_heart_beat();
  39. // this.heart_beat_id = setInterval(() => {
  40. // this.send_cmd(2, 4);
  41. // }, this.heart_beat_time * 1000);
  42. }
  43. // 停止心跳检测
  44. _stop_check_heart_beat() {
  45. clearInterval(this.heart_beat_id);
  46. this.heart_beat_id = null;
  47. }
  48. _on_recv_data(event) {
  49. var str_or_buf = event.data;
  50. if (!this.serivces_handler) {
  51. return;
  52. }
  53. var cmd = proto_man.decode_cmd(this.proto_type, str_or_buf);
  54. if (!cmd) {
  55. return;
  56. }
  57. var stype = cmd[0];
  58. var ctype = cmd[1];
  59. if (stype === 2 && ctype === 4) {
  60. // 收到心跳包,心跳包处理在此处理
  61. console.log("收到心跳包处理");
  62. this._start_check_heart_beat();
  63. }
  64. if (this.serivces_handler[stype]) {
  65. this.serivces_handler[stype](cmd[0], cmd[1], cmd[2]);
  66. }
  67. }
  68. _on_socket_close(event) {
  69. if (this.sock) {
  70. this.close();
  71. }
  72. }
  73. _on_socket_err(event) {
  74. this.close();
  75. }
  76. connect(url, proto_type) {
  77. this.sock = new WebSocket(url);
  78. this.sock.binaryType = "arraybuffer";
  79. this.sock.onopen = this._on_opened.bind(this);
  80. this.sock.onmessage = this._on_recv_data.bind(this);
  81. this.sock.onclose = this._on_socket_close.bind(this);
  82. this.sock.onerror = this._on_socket_err.bind(this);
  83. this.proto_type = proto_type;
  84. }
  85. send_cmd(stype, ctype, body) {
  86. if (!this.sock || !this.is_connected) {
  87. // 重新连接 socket
  88. let cache = [];
  89. cache[0] = stype;
  90. cache[1] = ctype;
  91. cache[2] = body;
  92. this.cache_send_cmd.push(cache);
  93. this.connect("ws://127.0.0.1:6081/ws", proto_man.PROTO_JSON);
  94. return;
  95. }
  96. console.log(`stype:${stype}, ctype:${ctype}, body:${body}`);
  97. var buf = proto_man.encode_cmd(this.proto_type, stype, ctype, body);
  98. this.sock.send(buf);
  99. this._start_check_heart_beat();
  100. }
  101. close() {
  102. this.is_connected = false;
  103. this._stop_check_heart_beat();
  104. if (this.sock !== null) {
  105. this.sock.close();
  106. this.sock = null;
  107. }
  108. }
  109. register_serivces_handler(serivces_handler) {
  110. this.serivces_handler = serivces_handler;
  111. }
  112. }