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:
commit
5c5b5dded6
10
package-lock.json
generated
10
package-lock.json
generated
@ -15,6 +15,7 @@
|
|||||||
"@types/node": "^20.8.9",
|
"@types/node": "^20.8.9",
|
||||||
"@types/socket.io-client": "^3.0.0",
|
"@types/socket.io-client": "^3.0.0",
|
||||||
"buffer": "^6.0.3",
|
"buffer": "^6.0.3",
|
||||||
|
"events": "^3.3.0",
|
||||||
"parcel": "^2.10.1",
|
"parcel": "^2.10.1",
|
||||||
"prettier": "3.0.3",
|
"prettier": "3.0.3",
|
||||||
"process": "^0.11.10",
|
"process": "^0.11.10",
|
||||||
@ -2721,6 +2722,15 @@
|
|||||||
"node": ">=0.8.0"
|
"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": {
|
"node_modules/fill-range": {
|
||||||
"version": "7.0.1",
|
"version": "7.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
"@types/node": "^20.8.9",
|
"@types/node": "^20.8.9",
|
||||||
"@types/socket.io-client": "^3.0.0",
|
"@types/socket.io-client": "^3.0.0",
|
||||||
"buffer": "^6.0.3",
|
"buffer": "^6.0.3",
|
||||||
|
"events": "^3.3.0",
|
||||||
"parcel": "^2.10.1",
|
"parcel": "^2.10.1",
|
||||||
"prettier": "3.0.3",
|
"prettier": "3.0.3",
|
||||||
"process": "^0.11.10",
|
"process": "^0.11.10",
|
||||||
|
10
src/index.ts
10
src/index.ts
@ -7,7 +7,15 @@ async function start() {
|
|||||||
true,
|
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");
|
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 Utils from "./utils";
|
||||||
import io from "socket.io-client";
|
import io from "socket.io-client";
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
|
||||||
export default class LinxSdkModule {
|
export default class LinxSdkModule {
|
||||||
utils: Utils;
|
utils: Utils;
|
||||||
user: UserData;
|
user: UserData;
|
||||||
groups: any;
|
groups: any;
|
||||||
#socket: any;
|
#socket: any;
|
||||||
|
socket: any;
|
||||||
|
handler = new EventEmitter();
|
||||||
|
|
||||||
constructor(apiKey: string, host: string, debug: boolean = false) {
|
constructor(apiKey: string, host: string, debug: boolean = false) {
|
||||||
this.utils = new Utils(apiKey, host, debug);
|
this.utils = new Utils(apiKey, host, debug);
|
||||||
@ -18,7 +21,7 @@ export default class LinxSdkModule {
|
|||||||
login: user,
|
login: user,
|
||||||
password: password,
|
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.user = login.data;
|
||||||
this.utils.setToken(this.user.token);
|
this.utils.setToken(this.user.token);
|
||||||
this.groups = await this.getGroups();
|
this.groups = await this.getGroups();
|
||||||
@ -30,20 +33,29 @@ export default class LinxSdkModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
models() {
|
||||||
|
enum USER_EVENTS {
|
||||||
|
CONNECT = "connect",
|
||||||
|
}
|
||||||
|
return USER_EVENTS;
|
||||||
|
}
|
||||||
|
|
||||||
establishWsConnection() {
|
establishWsConnection() {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
const hubAddress = this.user.account.configuration.hub_address;
|
const hubAddress = this.user.account.configuration.hub_address;
|
||||||
|
|
||||||
this.#socket = io(hubAddress, { rejectUnauthorized: false, secure: true });
|
this.#socket = io(hubAddress, { rejectUnauthorized: false, secure: true });
|
||||||
|
|
||||||
this.#socket.on("connect", () => {
|
this.#socket.on(Event.CONNECT, () => {
|
||||||
this.sendArs(true);
|
this.sendArs(true);
|
||||||
this.utils.log("The connection with the WS server was successfully established");
|
this.utils.log("The connection with the WS server was successfully established");
|
||||||
|
this.handler.emit(Event.CONNECT);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
["connect_timeout", "connect_error", "disconnect"].forEach((handler) => {
|
[Event.CONNECT_ERROR, Event.CONNECT_TIMEOUT, Event.DISCONNECT].forEach((eventType) => {
|
||||||
this.#socket.on(handler, (message: string) => {
|
this.#socket.on(eventType, (message: string) => {
|
||||||
|
this.handler.emit(eventType);
|
||||||
reject(new Error("Failed to initialize HUB connection"));
|
reject(new Error("Failed to initialize HUB connection"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -3,6 +3,13 @@ export enum Route {
|
|||||||
GROUPS = "/api/sdk/account/$1/groups",
|
GROUPS = "/api/sdk/account/$1/groups",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum Event {
|
||||||
|
CONNECT = "connect",
|
||||||
|
CONNECT_TIMEOUT = "connect_timeout",
|
||||||
|
CONNECT_ERROR = "connect_error",
|
||||||
|
DISCONNECT = "disconnect",
|
||||||
|
}
|
||||||
|
|
||||||
export enum Method {
|
export enum Method {
|
||||||
GET,
|
GET,
|
||||||
POST,
|
POST,
|
||||||
|
Loading…
Reference in New Issue
Block a user