Skip to content

Commit

Permalink
feat: socketio (#19)
Browse files Browse the repository at this point in the history
* feat: socketio for rpc
  • Loading branch information
amper-fb authored Jan 24, 2024
1 parent 6a93236 commit ff74d0c
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 126 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"node-forge": "^1.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"socket.io-client": "^4.7.3",
"react-qr-code": "^2.0.12"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Assets: React.FC = () => {
};

const onRefreshClicked = async () => {
setIsRefreshing(true)
setIsRefreshing(true);
try {
await refreshAccounts();
await refreshSupportedAssets(0);
Expand Down
5 changes: 4 additions & 1 deletion src/components/FireblocksNCWInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export const FireblocksNCWInitializer: React.FC = () => {

return (
<>
<Card title={`Fireblocks SDK (${ENV_CONFIG.NCW_SDK_ENV}) - Version ${fireblocksNCWSdkVersion}`} actions={sdkActions}>
<Card
title={`Fireblocks SDK (${ENV_CONFIG.NCW_SDK_ENV}) - Version ${fireblocksNCWSdkVersion}`}
actions={sdkActions}
>
<div>{fireblocksNCWStatus === "sdk_initialization_failed" && "Initialization Failed"}</div>
</Card>
<AreYouSureDialog
Expand Down
22 changes: 19 additions & 3 deletions src/services/ApiService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IBackupInfo, INewTransactionData, IPassphraseInfo } from "../IAppState";
import { IAuthManager } from "../auth/IAuthManager";
import { Manager, Socket, io } from "socket.io-client";

export type TTransactionStatus =
| "PENDING_SIGNATURE"
Expand Down Expand Up @@ -166,6 +167,9 @@ export class ApiService {
private _disposed: boolean = false;
private _pollingTxsActive: Map<string, boolean> = new Map();

private manager: Manager;
private socket: Socket;

constructor(
baseUrl: string,
private readonly authManager: IAuthManager,
Expand All @@ -174,6 +178,14 @@ export class ApiService {
if (this._baseUrl.endsWith("/")) {
this._baseUrl = this._baseUrl.slice(0, -1);
}

this.manager = new Manager(this._baseUrl, { autoConnect: true });
this.socket = this.manager.socket("/", {
auth: async (cb) => cb({ token: await this.authManager.getAccessToken() }),
});

this.socket.on("connect", () => console.log("websocket connected"));
this.socket.on("disconnect", () => console.log("websocket disconnected"));
}

public async login(): Promise<string> {
Expand Down Expand Up @@ -223,8 +235,12 @@ export class ApiService {
return response.walletId;
}

public sendMessage(deviceId: string, message: string): Promise<any> {
return this._postCall(`api/devices/${deviceId}/rpc`, { message });
public async sendMessage(deviceId: string, message: string): Promise<any> {
if (this.socket.connected) {
return await this.socket.emitWithAck("rpc", deviceId, message);
} else {
return this._postCall(`api/devices/${deviceId}/rpc`, { message });
}
}

public async getWeb3Connections(deviceId: string): Promise<IWeb3Session[]> {
Expand Down Expand Up @@ -253,7 +269,7 @@ export class ApiService {
}

public async createTransaction(deviceId: string, dataToSend?: INewTransactionData): Promise<ITransactionData> {
const createTxResponse = await this._postCall(`api/devices/${deviceId}/transactions`, dataToSend );
const createTxResponse = await this._postCall(`api/devices/${deviceId}/transactions`, dataToSend);
return createTxResponse;
}

Expand Down
Loading

0 comments on commit ff74d0c

Please sign in to comment.