Merge pull request 'LINXD-2559-sdk-part-1-auth-connect-events: Add event handler for client' (#1) from LINXD-2559-sdk-part-1-auth-connect-events into main

Reviewed-on: #1
Reviewed-by: Cristi Ene <cristi.ene@safemobile.com>
This commit is contained in:
Sergiu Toma 2023-12-06 11:09:01 +00:00
commit 5c5b5dded6
5 changed files with 44 additions and 6 deletions

10
package-lock.json generated
View File

@ -15,6 +15,7 @@
"@types/node": "^20.8.9",
"@types/socket.io-client": "^3.0.0",
"buffer": "^6.0.3",
"events": "^3.3.0",
"parcel": "^2.10.1",
"prettier": "3.0.3",
"process": "^0.11.10",
@ -2721,6 +2722,15 @@
"node": ">=0.8.0"
}
},
"node_modules/events": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz",
"integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==",
"dev": true,
"engines": {
"node": ">=0.8.x"
}
},
"node_modules/fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",

View File

@ -13,6 +13,7 @@
"@types/node": "^20.8.9",
"@types/socket.io-client": "^3.0.0",
"buffer": "^6.0.3",
"events": "^3.3.0",
"parcel": "^2.10.1",
"prettier": "3.0.3",
"process": "^0.11.10",

View File

@ -7,7 +7,15 @@ async function start() {
true,
);
[linxSdk.models().CONNECT].forEach((eventType: string): void => {
linxSdk.handler.on(eventType, (message: string): void => {
console.log("User event handler", eventType);
});
});
await linxSdk.init("AIRWizard-sergiu", "Safemobile123");
}
start();
start().then(() => {
// ...
});

View File

@ -1,12 +1,15 @@
import { LogLevel, Method, Route, UserData, UserResponse } from "./models";
import { Event, LogLevel, Method, Route, UserData, UserResponse } from "./models";
import Utils from "./utils";
import io from "socket.io-client";
import { EventEmitter } from "events";
export default class LinxSdkModule {
utils: Utils;
user: UserData;
groups: any;
#socket: any;
socket: any;
handler = new EventEmitter();
constructor(apiKey: string, host: string, debug: boolean = false) {
this.utils = new Utils(apiKey, host, debug);
@ -18,7 +21,7 @@ export default class LinxSdkModule {
login: user,
password: password,
});
if (!login.success) throw new Error("Failed to initialize SDK");
if (!login.success) new Error("Failed to initialize SDK");
this.user = login.data;
this.utils.setToken(this.user.token);
this.groups = await this.getGroups();
@ -30,20 +33,29 @@ export default class LinxSdkModule {
}
}
models() {
enum USER_EVENTS {
CONNECT = "connect",
}
return USER_EVENTS;
}
establishWsConnection() {
return new Promise<void>((resolve, reject) => {
const hubAddress = this.user.account.configuration.hub_address;
this.#socket = io(hubAddress, { rejectUnauthorized: false, secure: true });
this.#socket.on("connect", () => {
this.#socket.on(Event.CONNECT, () => {
this.sendArs(true);
this.utils.log("The connection with the WS server was successfully established");
this.handler.emit(Event.CONNECT);
resolve();
});
["connect_timeout", "connect_error", "disconnect"].forEach((handler) => {
this.#socket.on(handler, (message: string) => {
[Event.CONNECT_ERROR, Event.CONNECT_TIMEOUT, Event.DISCONNECT].forEach((eventType) => {
this.#socket.on(eventType, (message: string) => {
this.handler.emit(eventType);
reject(new Error("Failed to initialize HUB connection"));
});
});

View File

@ -3,6 +3,13 @@ export enum Route {
GROUPS = "/api/sdk/account/$1/groups",
}
export enum Event {
CONNECT = "connect",
CONNECT_TIMEOUT = "connect_timeout",
CONNECT_ERROR = "connect_error",
DISCONNECT = "disconnect",
}
export enum Method {
GET,
POST,