LINXD-2559-sdk-part-1-auth-connect-events: Add event handler for client #1
10
package-lock.json
generated
10
package-lock.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
10
src/index.ts
10
src/index.ts
@ -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(() => {
|
||||
// ...
|
||||
});
|
||||
|
@ -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"));
|
||||
});
|
||||
});
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user
I chose to have two types of models:
The first one to use inside the SDK, is to have all the events in an enum.
The 2nd is in the SDK module and is accessible to the client through linxSdk.models().<event_name>.
I chose this approach because we offer the client a predefined list of events we interface with, but not all the events with which the SDK works. Another reason is security.