Fix Web Sockets. Form into library. Move demo to demo folder. Split demo between sse and websocket.
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
import { PrivateKey } from "@xo-cash/primitives";
|
||||
import { toExtendedJson } from "@xo-cash/utils";
|
||||
|
||||
import { SSEClient } from '../src/sse/client.js';
|
||||
|
||||
const testSSE = async () => {
|
||||
const privateKey = PrivateKey.fromString('c440ce6ac5b63ae7bc4af9891b80b7a88ccdeebba5e43246885e9ec56f912ac2');
|
||||
const client = new SSEClient('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
|
||||
await client.subscribe('test');
|
||||
|
||||
// Write a new value to the resource
|
||||
await client.write('test', {'message': 'Hello, world! this is the minimal sync client using SSE'});
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
||||
testSSE();
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user