WXWsClient.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**Wxweb scoket 客户端*/
  2. export class WXWsClient {
  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 = wx.connectSocket({
  48. url: url,
  49. header: {
  50. 'content-type': 'application/json'
  51. }
  52. })
  53. ws.onClose((res) => {
  54. this._ws = null;
  55. this.onClosed(res.code, res.reason);
  56. });
  57. ws.onError((res) => {
  58. this.onError(res.errMsg);
  59. });
  60. ws.onMessage((res) => {
  61. this.onMessage(res.data);
  62. })
  63. ws.onOpen((res) => {
  64. console.log(res.header);
  65. console.log(res.profile);
  66. this.onConnected();
  67. })
  68. this._ws = ws;
  69. } catch (error) {
  70. this.onError(error instanceof Error ? error.message : 'Unknown error');
  71. }
  72. return true;
  73. }
  74. private _closing: boolean = false;
  75. /**
  76. * 主动关闭WebSocket连接
  77. */
  78. close(code?: number, reason?: string): void {
  79. if (this.isClosed || this.isClosing) return;
  80. this._closing = true;
  81. this.onClosing();
  82. this._ws.close({
  83. code: code,
  84. reason: reason,
  85. success: (res) => {
  86. // 关闭成功的回调
  87. chsdk.log.log("close success", res);
  88. },
  89. fail: (res) => {
  90. // 关闭失败的回调
  91. chsdk.log.log("close fail", res);
  92. },
  93. });
  94. }
  95. /**
  96. * 发送数据
  97. * @param data 指定格式数据
  98. */
  99. send(data: string | ArrayBuffer): void {
  100. this._ws.send({
  101. data: data,
  102. success: (ret) => { console.log('成功', ret) },
  103. fail: (ret) => { console.log('失败', ret) },
  104. complete: (ret) => { console.log('完成', ret) },
  105. })
  106. }
  107. }