WsClient.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**web scoket 客户端*/
  2. export class WsClient {
  3. public onConnected(evt: any): void { };
  4. public onError(err: any): void { };
  5. public onClosing(): void { };
  6. public onClosed(event: CloseEvent): void { };
  7. public onMessage(msg: MessageEvent): void { };
  8. //
  9. private _ws: WebSocket | 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. private _onConnected(evt: any) {
  22. this.onConnected(evt);
  23. }
  24. /**收到消息时的回调 */
  25. private _onMessage(msg: MessageEvent) {
  26. this.onMessage(msg);
  27. }
  28. /** 错误处理回调 */
  29. private _onError(err: any) {
  30. this.onError(err);
  31. }
  32. /**连接关闭时的回调 */
  33. private _onClosed(event: CloseEvent) {
  34. this.onClosed(event);
  35. }
  36. /**
  37. * 获取当前连接状态
  38. * @returns 是否处于活动状态
  39. */
  40. get isActive(): boolean {
  41. return this._ws?.readyState === WebSocket.OPEN;
  42. }
  43. /**
  44. * 检查是否正在连接
  45. * @returns 是否正在连接
  46. */
  47. private get isConnecting(): boolean {
  48. return this._ws?.readyState === WebSocket.CONNECTING;
  49. }
  50. /**
  51. *是否正在关闭
  52. */
  53. private get isClosing(): boolean {
  54. return this._ws?.readyState === WebSocket.CLOSING;
  55. }
  56. /**是否已经关闭 */
  57. private get isClosed(): boolean {
  58. return this._ws?.readyState === WebSocket.CLOSED;
  59. }
  60. /**手动连接*/
  61. connect() {
  62. if (this.isConnecting) return false;
  63. if (this.isActive) return false;
  64. const url = this._url;
  65. if (!url) return false;
  66. try {
  67. //eslint-disable-next-line @typescript-eslint/ban-ts-comment
  68. //@ts-ignore
  69. //eslint-disable-next-line @typescript-eslint/no-unsafe-call
  70. const ws = this._ca && url.startsWith('wss://') ? new WebSocket(url, {}, this._ca) : new WebSocket(url)
  71. ws.binaryType = 'arraybuffer';
  72. ws.onmessage = this._onMessage.bind(this);
  73. ws.onopen = this._onConnected.bind(this);
  74. ws.onerror = this._onError.bind(this);
  75. ws.onclose = this._onClosed.bind(this);
  76. this._ws = ws;
  77. } catch (error) {
  78. this._onError(error instanceof Error ? error.message : 'Unknown error');
  79. }
  80. return true;
  81. }
  82. /**
  83. * 主动关闭WebSocket连接
  84. */
  85. close(code?: number, reason?: string): void {
  86. if (this.isClosed || this.isClosing) return;
  87. this.onClosing();
  88. this._ws.close(code, reason);
  89. }
  90. /**
  91. * 发送数据
  92. * @param data 指定格式数据
  93. */
  94. send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
  95. this._ws.send(data);
  96. }
  97. }