Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 2x 2x 2x 3x 6x 3x 1x 1x 1x 1x 1x 1x 1x 3x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 2x 2x 2x 6x 3x 2x 2x 2x 2x 2x 2x 3x 6x 1x | import { io, ManagerOptions, Socket } from "socket.io-client";
import {
SocketConnection,
SocketConnectionStatus,
} from "~/types/SocketConnection";
import { SocketEvents } from "~/types/SocketEvents";
import { HubSocketActions } from "../../interfaces/HubSocketActions";
import StateManager from "../state/StateManager";
/**
* A service for managing the socket connection to a hub.
* It handles establishing, reconnecting, and closing the socket connection,
* along with handling socket events related to device status.
*/
class HubSocketService implements HubSocketActions {
private socket: Socket | undefined;
private socketStatus = SocketConnectionStatus;
private events = SocketEvents;
/**
* Creates an instance of HubSocketService.
*
* @param {StateManager} stateManager - The state manager used to manage application state.
* @param {string} socketEndpoint - The URL endpoint for the socket connection.
*/
constructor(
private stateManager: StateManager,
private socketEndpoint: string,
) {}
/**
* Establishes a connection to the hub's socket endpoint.
* The connection is authenticated with the current token stored in the token store.
* If the token is invalid, the connection is not established.
*
* @param {Partial<ManagerOptions>=} opts - Optional partial options for the socket.io connection.
* @returns {Promise<void>} A promise that resolves when the connection is established.
*/
public open = async (opts?: Partial<ManagerOptions>): Promise<void> => {
const token = this.stateManager.getToken();
if (token) {
await this.connect(this.socketEndpoint, token, opts);
this.setupReconnectListeners();
}
};
/**
* Closes the current socket connection and updates the connection status to 'disconnected'.
* Removes all listeners from the socket before closing.
*/
public close = () => {
if (this.socket) {
this.socket.removeAllListeners();
this.socket.close();
this.stateManager.setConnectionState(
this.socketStatus.DISCONNECTED,
);
this.socket = undefined;
}
};
/**
* Establishes a connection to the hub's socket endpoint.
* The connection is authenticated with the provided token.
* If the connection is already established, it is closed first.
*
* @param {string} url - The URL of the socket endpoint.
* @param {string} token - The authentication token to use.
* @param {Partial<ManagerOptions>=} opts - Optional partial options for the socket.io connection.
*/
private connect = async (
url: string,
token: string,
opts?: Partial<ManagerOptions>,
) => {
this.close();
const connection = await this.establishConnection(url, token, opts);
this.socket = connection.socket;
this.stateManager.setConnectionState(connection.status);
};
/**
* Initializes a socket connection to the specified URL with the given token and options.
* Resolves with a SocketConnection object containing the socket instance and the connection status.
*
* @param {string} url - The URL to which the socket connection is established.
* @param {string} token - The session token to use.
* @param {Partial<ManagerOptions>=} opts - Optional partial options for the socket.io connection.
* @returns {Promise<SocketConnection>} A promise that resolves with a SocketConnection object.
*/
private establishConnection = (
url: string,
token: string,
opts?: Partial<ManagerOptions>,
): Promise<SocketConnection> => {
return new Promise((resolve) => {
const socket: Socket = io(url, {
auth: { token },
...opts,
});
socket.on(this.events.DEVICES_STATUS, (data) => {
this.stateManager.setDevices(data.reportedDevices);
resolve(<SocketConnection>{
socket,
status: this.socketStatus.CONNECTED,
});
});
socket.on(this.events.CONNECT_ERROR, () => {
resolve(<SocketConnection>{
status: this.socketStatus.ERROR,
});
});
});
};
/**
* Sets up listeners for socket reconnection events.
* Updates the connection status to 'connected' on successful reconnection
* and to 'error' if reconnection fails.
*/
private setupReconnectListeners = (): void => {
if (!this.socket) return;
this.socket.off(this.events.RECONNECT);
this.socket.off(this.events.RECONNECT_FAIL);
this.socket.on(this.events.RECONNECT, () => {
this.stateManager.setConnectionState(this.socketStatus.CONNECTED);
});
this.socket.on(this.events.RECONNECT_FAIL, () => {
this.stateManager.setConnectionState(this.socketStatus.ERROR);
});
};
}
export default HubSocketService;
|