Add websockets.ts
This commit is contained in:
parent
47c356a22f
commit
6128f826b1
@ -1,5 +1,6 @@
|
|||||||
import { createUserReturn, User, Process } from '../dist/pkg/sdk_client';
|
import { createUserReturn, User, Process } from '../dist/pkg/sdk_client';
|
||||||
import IndexedDB from './database'
|
import IndexedDB from './database'
|
||||||
|
import { WebSocketClient } from './websockets';
|
||||||
|
|
||||||
class Services {
|
class Services {
|
||||||
private static instance: Services;
|
private static instance: Services;
|
||||||
@ -162,6 +163,18 @@ class Services {
|
|||||||
return imageBytes;
|
return imageBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async parseBitcoinMessage(raw: Blob): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const buffer = await raw.arrayBuffer();
|
||||||
|
const uint8Array = new Uint8Array(buffer);
|
||||||
|
const msg: string = this.sdkClient.parse_bitcoin_network_msg(uint8Array);
|
||||||
|
return msg;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error processing the blob:", error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async displayRevoke(): Promise<void> {
|
public async displayRevoke(): Promise<void> {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
services.injectHtml('REVOKE');
|
services.injectHtml('REVOKE');
|
||||||
@ -200,6 +213,11 @@ class Services {
|
|||||||
services.attachSubmitListener("form4nk", services.updateAnId);
|
services.attachSubmitListener("form4nk", services.updateAnId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async parse4nkMessage(raw: string): Promise<string | null> {
|
||||||
|
const msg: string = this.sdkClient.parse_4nk_msg(raw);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
public injectUpdateAnIdHtml(bodyToInject: string, styleToInject: string, scriptToInject: string) {
|
public injectUpdateAnIdHtml(bodyToInject: string, styleToInject: string, scriptToInject: string) {
|
||||||
console.log("JS html : "+bodyToInject);
|
console.log("JS html : "+bodyToInject);
|
||||||
const body = document.getElementsByTagName('body')[0];
|
const body = document.getElementsByTagName('body')[0];
|
||||||
@ -263,6 +281,16 @@ class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async checkTransaction(tx: string): Promise<string | null> {
|
||||||
|
const services = await Services.getInstance();
|
||||||
|
try {
|
||||||
|
return services.sdkClient.check_network_transaction(tx);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async getAllProcessForUser(pre_id: string): Promise<Process[]> {
|
public async getAllProcessForUser(pre_id: string): Promise<Process[]> {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
let user: User;
|
let user: User;
|
||||||
@ -401,6 +429,16 @@ class Services {
|
|||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public obtainTokenWithFaucet(wsclient: WebSocketClient, spaddress: string): string | null {
|
||||||
|
try {
|
||||||
|
wsclient.sendMessage(spaddress);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to obtain tokens with relay ", wsclient.getUrl());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Services;
|
export default Services;
|
||||||
|
85
src/websockets.ts
Normal file
85
src/websockets.ts
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
import Services from "./services";
|
||||||
|
// import * as mempool from "./mempool";
|
||||||
|
|
||||||
|
class WebSocketClient {
|
||||||
|
private ws: WebSocket;
|
||||||
|
private messageQueue: string[] = [];
|
||||||
|
|
||||||
|
constructor(url: string, private services: Services) {
|
||||||
|
this.ws = new WebSocket(url);
|
||||||
|
|
||||||
|
this.ws.addEventListener('open', (event) => {
|
||||||
|
console.log('WebSocket connection established');
|
||||||
|
// Once the connection is open, send all messages in the queue
|
||||||
|
while (this.messageQueue.length > 0) {
|
||||||
|
const message = this.messageQueue.shift();
|
||||||
|
if (message) {
|
||||||
|
this.ws.send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for messages
|
||||||
|
this.ws.addEventListener('message', (event) => {
|
||||||
|
const msgData = event.data;
|
||||||
|
console.log(msgData);
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
if (msgData instanceof Blob) {
|
||||||
|
// bitcoin network msg is just bytes
|
||||||
|
let res = await services.parseBitcoinMessage(msgData);
|
||||||
|
if (res) {
|
||||||
|
let ours = await services.checkTransaction(res);
|
||||||
|
if (ours) {
|
||||||
|
console.log("Found our utxo in "+res);
|
||||||
|
} else {
|
||||||
|
console.log("No utxo found in tx "+res);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error("Faile to parse a bitcoin network msg");
|
||||||
|
}
|
||||||
|
} else if (typeof(msgData) === 'string') {
|
||||||
|
// json strings are 4nk message
|
||||||
|
console.log("Received text message: "+msgData);
|
||||||
|
let res = await services.parse4nkMessage(msgData);
|
||||||
|
if (res) {
|
||||||
|
console.debug(res);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error("Received an unknown message");
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for possible errors
|
||||||
|
this.ws.addEventListener('error', (event) => {
|
||||||
|
console.error('WebSocket error:', event);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for when the connection is closed
|
||||||
|
this.ws.addEventListener('close', (event) => {
|
||||||
|
console.log('WebSocket is closed now.');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to send messages
|
||||||
|
public sendMessage(message: string): void {
|
||||||
|
if (this.ws.readyState === WebSocket.OPEN) {
|
||||||
|
this.ws.send(message);
|
||||||
|
} else {
|
||||||
|
console.error('WebSocket is not open. ReadyState:', this.ws.readyState);
|
||||||
|
this.messageQueue.push(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUrl(): string {
|
||||||
|
return this.ws.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to close the WebSocket connection
|
||||||
|
public close(): void {
|
||||||
|
this.ws.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { WebSocketClient };
|
Loading…
x
Reference in New Issue
Block a user