38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { PrivateKey } from "@xo-cash/primitives";
|
|
import { toExtendedJson } from "@xo-cash/utils";
|
|
|
|
import { WsClient } from '../src/ws/client.js';
|
|
|
|
const testWS = async () => {
|
|
const privateKey = PrivateKey.fromString('c440ce6ac5b63ae7bc4af9891b80b7a88ccdeebba5e43246885e9ec56f912ac2');
|
|
const client = new WsClient('https://v2.sync.xo.harvmaster.com', privateKey, {
|
|
onMessage: (message) => {
|
|
console.log(toExtendedJson(message));
|
|
},
|
|
onError: (error) => {
|
|
console.error(error);
|
|
},
|
|
});
|
|
|
|
// Connect to the sync server
|
|
await client.connect();
|
|
|
|
// Subscribe to the resource
|
|
client.subscribe('test');
|
|
|
|
// Write a new value to the resource
|
|
await client.write('test', {'message': 'Hello, world! this is the minimal sync client using WS'});
|
|
|
|
// Read the current value of the resource
|
|
const data = await client.read('test');
|
|
console.log(data);
|
|
|
|
// If the user presses Ctrl+C, disconnect the client and exit the program
|
|
process.on('SIGINT', async () => {
|
|
await client.disconnect();
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
testWS();
|