TTWsClient.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**tt scoket 客户端*/
  2. export class TTWsClient {
  3. public onConnected(): void { };
  4. public onError(err: string): void { };
  5. public onClosing(): void { };
  6. public onClosed(code: number, reason: string): void { };
  7. public onMessage(msg: string | ArrayBuffer): void { };
  8. //
  9. private _ws: any | null = null;
  10. /** WebSocket对象*/get ws() { return this._ws };
  11. /** 连接地址*/
  12. private _url: string | null = null;
  13. /**证书*/
  14. private _ca?: string;
  15. constructor(url: string, ca?: string, autoConnect?: boolean) {
  16. this._ca = ca;
  17. this._url = url;
  18. if (autoConnect) this.connect();
  19. }
  20. /**
  21. * 获取当前连接状态
  22. * @returns 是否处于活动状态
  23. */
  24. get isActive(): boolean {
  25. return this._ws != null;
  26. }
  27. /**
  28. *是否正在关闭
  29. */
  30. private get isClosing(): boolean {
  31. return this._closing;
  32. }
  33. /**是否已经关闭 */
  34. private get isClosed(): boolean {
  35. return this._ws === null;
  36. }
  37. /**手动连接*/
  38. connect() {
  39. if (this.isActive) return false;
  40. const url = this._url;
  41. if (!url) return false;
  42. try {
  43. this.close();
  44. //eslint-disable-next-line @typescript-eslint/ban-ts-comment
  45. //@ts-ignore
  46. //eslint-disable-next-line @typescript-eslint/no-unsafe-call
  47. const ws = tt.connectSocket({
  48. url: url,
  49. success: (res) => {
  50. chsdk.log.info("WebSocket创建成功", res);
  51. },
  52. fail: (res) => {
  53. chsdk.log.info("WebSocket创建失败", res);
  54. },
  55. });
  56. ws.onOpen(() => {
  57. chsdk.log.info("WebSocket 已连接");
  58. this.onConnected();
  59. });
  60. ws.onClose((res) => {
  61. this._ws = null;
  62. this.onClosed(res?.code, res?.reason);
  63. chsdk.log.info("WebSocket 已断开");
  64. });
  65. ws.onError((error) => {
  66. chsdk.log.info("WebSocket 发生错误:", error);
  67. this.onError(error);
  68. });
  69. ws.onMessage((message) => {
  70. chsdk.log.info("socket message:", message);
  71. this.onMessage(message.data);
  72. });
  73. this._ws = ws;
  74. } catch (error) {
  75. this.onError(error instanceof Error ? error.message : 'Unknown error');
  76. }
  77. return true;
  78. }
  79. private _closing: boolean = false;
  80. /**
  81. * 主动关闭WebSocket连接
  82. */
  83. close(code?: number, reason?: string): void {
  84. if (this.isClosed || this.isClosing) return;
  85. this._closing = true;
  86. this.onClosing();
  87. this._ws.close({
  88. code: code,
  89. reason: reason,
  90. success: (res) => {
  91. // 关闭成功的回调
  92. chsdk.log.info("close success", res);
  93. },
  94. fail: (res) => {
  95. // 关闭失败的回调
  96. chsdk.log.info("close fail", res);
  97. },
  98. });
  99. }
  100. /**
  101. * 发送数据
  102. * @param data 指定格式数据
  103. */
  104. send(data: string | ArrayBuffer): void {
  105. this._ws.send({
  106. data: data,
  107. success: (ret) => { chsdk.log.info('成功', ret) },
  108. fail: (ret) => { chsdk.log.info('失败', ret) },
  109. complete: (ret) => { chsdk.log.info('完成', ret) },
  110. })
  111. }
  112. }