websocket-client.d.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { VirtualWebSocketClient } from './virtual-websocket-client';
  2. import { IDatabaseServiceContext, IWatchOptions, DBRealtimeListener } from '../typings/index';
  3. import { IRequestMessage } from '../typings/realtime';
  4. import { CLOSE_EVENT_CODE } from './ws-event';
  5. export interface IRealtimeWebSocketClientConstructorOptions {
  6. maxReconnect?: number;
  7. reconnectInterval?: number;
  8. context: IDatabaseServiceContext;
  9. }
  10. export interface ISignature {
  11. envId: string;
  12. secretVersion: number;
  13. signStr: string;
  14. wsUrl: string;
  15. expireTS: number;
  16. }
  17. export interface ILoginInfo {
  18. loggedIn: boolean;
  19. loggingInPromise?: Promise<ILoginResult>;
  20. loginStartTS?: number;
  21. loginResult?: ILoginResult;
  22. }
  23. export interface ILoginResult {
  24. envId: string;
  25. }
  26. export interface IWSSendOptions {
  27. msg: IRequestMessage;
  28. waitResponse?: boolean;
  29. skipOnMessage?: boolean;
  30. timeout?: number;
  31. }
  32. export interface IWSWatchOptions extends IWatchOptions {
  33. envId?: string;
  34. collectionName: string;
  35. query: string;
  36. limit?: number;
  37. orderBy?: Record<string, string>;
  38. }
  39. export declare class RealtimeWebSocketClient {
  40. private _virtualWSClient;
  41. private _queryIdClientMap;
  42. private _watchIdClientMap;
  43. private _maxReconnect;
  44. private _reconnectInterval;
  45. private _context;
  46. private _ws?;
  47. private _lastPingSendTS?;
  48. private _pingFailed;
  49. private _pongMissed;
  50. private _pingTimeoutId?;
  51. private _pongTimeoutId?;
  52. private _logins;
  53. private _wsInitPromise?;
  54. private _wsReadySubsribers;
  55. private _wsResponseWait;
  56. private _rttObserved;
  57. private _reconnectState;
  58. private _wsSign;
  59. constructor(options: IRealtimeWebSocketClientConstructorOptions);
  60. private initWebSocketConnection;
  61. private initWebSocketEvent;
  62. private isWSConnected;
  63. private onceWSConnected;
  64. private webLogin;
  65. private getWsSign;
  66. private getWaitExpectedTimeoutLength;
  67. private heartbeat;
  68. clearHeartbeat(): void;
  69. private ping;
  70. send: <T = any>(opts: IWSSendOptions) => Promise<T>;
  71. close(code: CLOSE_EVENT_CODE): void;
  72. closeAllClients: (error: any) => void;
  73. pauseClients: (clients?: Set<VirtualWebSocketClient>) => void;
  74. resumeClients: (clients?: Set<VirtualWebSocketClient>) => void;
  75. private onWatchStart;
  76. private onWatchClose;
  77. watch(options: IWSWatchOptions): DBRealtimeListener;
  78. }