import * as mqtt5_packet from "../../common/mqtt5_packet"; import * as mqtt5 from "../../common/mqtt5"; import * as mqtt_shared from "../../common/mqtt_shared"; import * as ws from "../ws"; import { CrtError } from "../error"; import { BufferedEventEmitter } from "../../common/event"; import { OfflineQueuePolicy, ConnectOptions, PublishOptions, PublishResult, PublishResultType, ResumeSessionPolicyType, SubscribeOptions, UnsubscribeOptions } from "./protocol"; export { OfflineQueuePolicy, ConnectOptions, PublishOptions, PublishResult, PublishResultType, ResumeSessionPolicyType, SubscribeOptions, UnsubscribeOptions }; /** * Emitted when the client begins a connection attempt */ export interface ConnectingEvent { } /** * Type signature for a function that handles ConnectingEvent events */ export type ConnectingEventListener = (eventData: ConnectingEvent) => void; /** * Emitted when the client successfully establishes an MQTT connection to the remote endpoint */ export interface ConnectionSuccessEvent { connack: mqtt5_packet.ConnackPacket; } /** * Type signature for a function that handles ConnectionSuccessEvent events */ export type ConnectionSuccessEventListener = (eventData: ConnectionSuccessEvent) => void; /** * Emitted when the client fails to establish an MQTT connection to the remote endpoint */ export interface ConnectionFailureEvent { error: CrtError; connack?: mqtt5_packet.ConnackPacket; } /** * Type signature for a function that handles ConnectionFailureEvent events */ export type ConnectionFailureEventListener = (eventData: ConnectionFailureEvent) => void; /** * Emitted when a successfully-established MQTT connection is interrupted for any reason. Can only follow * a ConnectionSuccessEvent. */ export interface DisconnectionEvent { error: CrtError; disconnect?: mqtt5_packet.DisconnectPacket; } /** * Type signature for a function that handles DisconnectionEvent events */ export type DisconnectionEventListener = (eventData: DisconnectionEvent) => void; /** * Emitted when the client enters the stopped state (no connection attempts will be made until restarted) */ export interface StoppedEvent { } /** * Type signature for a function that handles StoppedEvent events */ export type StoppedEventListener = (eventData: StoppedEvent) => void; /** * Emitted whenever the client receives a publish packet from the MQTT broker */ export interface PublishReceivedEvent { /** * Incoming Publish packet */ publish: mqtt5_packet.PublishPacket; /** * An object that allows the event recipient to take control of when the Publish packet's acknowledgement * packet is sent. If the acknowledgement handle is not acquired by an event listener during the emission * process, the client will automatically send the acknowledgement itself. * * Undefined if this publish is not acknowledgeable (QoS 0). */ acknowledgementControl?: mqtt_shared.PublishAcknowledgementHandleWrapper; } /** * Type signature for a function that handles PublishReceivedEvent events */ export type PublishReceivedEventListener = (eventData: PublishReceivedEvent) => void; /** * Controls how the client should automatically resubscribe to topics upon reconnection */ export declare enum ResubscribeModeType { /** * Do not attempt to resubscribe to topics under any circumstance */ Disabled = 0, /** * Always resubscribe to topics on reconnect, regardless of session resumption */ EnabledAlways = 1, /** * Only resubscribe to topics on reconnect if a session could not be rejoined */ EnabledOnSessionResumptionFail = 2, Default = 0 } /** * Client-relevant configuration options */ export interface ClientConfig { /** * What version of MQTT to use. */ protocolVersion: mqtt_shared.ProtocolMode; /** * How should queued packets be treated when the client is not connected? */ offlineQueuePolicy: OfflineQueuePolicy; /** * Configuration for the initial CONNECT packet sent by the client once the transport is established */ connectOptions: ConnectOptions; /** * Timeout, in milliseconds, to wait for a Pingresp after a Pingreq has been sent. If the timeout is breached, * the connection will be closed. */ pingTimeoutMillis?: number; /** * Function that creates a Websocket connection to the remote broker */ connectionFactory: () => Promise; /** * Overarching timeout for MQTT connection establishment. Failure to establish an MQTT connection by this timeout * results in */ connectTimeoutMillis: number; /** * How should the reconnection delay be randomized, if at all? */ retryJitterMode?: mqtt5.RetryJitterType; /** * Minimum amount of time, in milliseconds, to wait between reconnection attempts */ minReconnectDelayMs?: number; /** * Maximum amount of time, in milliseconds, to wait between reconnection attempts */ maxReconnectDelayMs?: number; /** * The length of time a successful connection must persist before we clear the reconnect attempts state. * This allows the client to persist its reconnect delay when connections are getting terminated shortly after * establishment. */ resetConnectionFailureCountMillis?: number; /** * What kind of resubscribe behavior should the use? */ resubscribeMode?: ResubscribeModeType; } /** * Internal MQTT client implementation that supports both 311 and 5. Restricted to match IoT Core's * feature set (no QoS 2 support). */ export declare class Client extends BufferedEventEmitter { private config; private protocolState; private desiredState; private currentState; private creationTime; private connection?; private connectionId; private pendingConnectionTimeout?; private nextConnectionId; private reconnectTimepoint?; private lastReconnectDelay?; private connectionFailureCount; private resetConnectionFailuresTimepoint?; private nextServiceTimepoint?; private serviceTask?; private inService; private socketWriteBuffer; private onConnectionClosedCallback; private onConnectionDataCallback; private connectionCallbackState; private resubscribeManager; constructor(config: ClientConfig); /** * Initiates the transition to a connected state if appropriate */ start(): void; /** * Initiates the transition to a stopped state if appropriate. * * If a disconnect packet is passed in and the client is currently connected, the client will delay connection * close until the packet can be flushed. * * @param disconnect - optional disconnect packet to send before closing an active connection */ stop(disconnect?: mqtt5_packet.DisconnectPacket): void; /** * Queues a publish packet to be sent to the remote broker. If successfully queued, the packet will be sent * as soon as it reaches the head of the queue while the client is connected. * * @param publish publish packet to send * @param options additional configuration options */ publish(publish: mqtt5_packet.PublishPacket, options?: PublishOptions): Promise; /** * Queues a subscribe packet to be sent to the remote broker. If successfully queued, the packet will be sent * as soon as it reaches the head of the queue while the client is connected. * * @param subscribe subscribe packet to send * @param options additional configuration options */ subscribe(subscribe: mqtt5_packet.SubscribePacket, options?: SubscribeOptions): Promise; /** * Queues an unsubscribe packet to be sent to the remote broker. If successfully queued, the packet will be sent * as soon as it reaches the head of the queue while the client is connected. * * @param unsubscribe unsubscribe packet to send * @param options additional configuration options */ unsubscribe(unsubscribe: mqtt5_packet.UnsubscribePacket, options?: UnsubscribeOptions): Promise; /** * Event emitted when the client begins a connection attempt * * Listener type: {@link ConnectingEventListener} * * @event */ static CONNECTING: string; /** * Event emitted when the client has successfully connected to the remote broker * * Listener type: {@link ConnectionSuccessEventListener} * * @event */ static CONNECTION_SUCCESS: string; /** * Event emitted when the client has failed to connect to the remote broker * * Listener type: {@link ConnectionFailureEventListener} * * @event */ static CONNECTION_FAILURE: string; /** * Event emitted when the client's connection has been closed * * Listener type: {@link DisconnectionEventListener} * * @event */ static DISCONNECTION: string; /** * Event emitted when the client enters the stopped state * * Listener type: {@link StoppedEventListener} * * @event */ static STOPPED: string; /** * Event emitted when the client receives a publish message from the broker * * Listener type: {@link PublishReceivedEventListener} * * @event */ static PUBLISH_RECEIVED: string; /** * Registers a listener for the client's {@link ConnectingEvent} event. A * {@link ConnectingEvent} event is emitted when the client initiates a connection attempt with * a remote broker. * * @param event the type of event to listen to * @param listener the event listener to add */ on(event: 'connecting', listener: ConnectingEventListener): this; /** * Registers a listener for the client's {@link ConnectionSuccessEvent} event. A * {@link ConnectionSuccessEvent} event is emitted when a successful CONNACK packet is received from the * broker at the conclusion of a connection attempt. * * @param event the type of event to listen to * @param listener the event listener to add */ on(event: 'connectionSuccess', listener: ConnectionSuccessEventListener): this; /** * Registers a listener for the client's {@link ConnectionFailureEvent} event. A * {@link ConnectionFailureEvent} event is emitted when a connection attempt fails. * * @param event the type of event to listen to * @param listener the event listener to add */ on(event: 'connectionFailure', listener: ConnectionFailureEventListener): this; /** * Registers a listener for the client's {@link DisconnectionEvent} event. A * {@link DisconnectionEvent} event is emitted when a successfully established connection is closed. * * @param event the type of event to listen to * @param listener the event listener to add */ on(event: 'disconnection', listener: DisconnectionEventListener): this; /** * Registers a listener for the client's {@link StoppedEvent} event. A * {@link StoppedEvent} event is emitted when the client enters the stopped state. * * @param event the type of event to listen to * @param listener the event listener to add */ on(event: 'stopped', listener: StoppedEventListener): this; /** * Registers a listener for the client's {@link PublishReceivedEvent} event. A * {@link PublishReceivedEvent} event is emitted every time the client receives a publish packet from the * broker. * * @param event the type of event to listen to * @param listener the event listener to add */ on(event: 'publishReceived', listener: PublishReceivedEventListener): this; private onSocketWrite; private service; private reevaluateService; private getCurrentTime; private changeState; private transitionToState; private transitionToStateConnecting; private transitionToStateConnected; private transitionToStatePendingReconnect; private transitionToStateStopped; private shutdownConnection; private onConnackReceivedEvent; private onPublishReceivedEvent; private onDisconnectReceivedEvent; private onProtocolStateHalted; private onConnectionClosed; private onConnectionData; private linkToConnection; private unlinkFromConnection; private clearConnectionCallbackState; private emitConnectionTerminationEvent; }