changed client example to not use socket.io

This commit is contained in:
Nour Alharithi
2023-10-29 14:08:46 -07:00
parent 3a4aa5be67
commit 547b8e85b9

View File

@@ -1,17 +1,26 @@
import { io } from 'socket.io-client';
import WebSocket from 'ws';
const ws = new WebSocket('ws://localhost:3000/ws');
const main = async () => {
const socket = io('http://localhost:3000');
socket.on('connect', () => {
socket.emit('subscribe', 'SOL-PERP');
});
socket.on('SOL-PERP', (data: any) => {
console.log(data);
});
};
main().then(() => {
console.log('running');
ws.on('open', () => {
console.log('Connected to the server');
ws.send(JSON.stringify({ type: 'subscribe', channel: 'SOL-PERP' }));
});
ws.on('message', (data: WebSocket.Data) => {
try {
const message = JSON.parse(data.toString());
if (message.channel === 'SOL-PERP') {
console.log('Received data:', message.data);
}
} catch (e) {
console.error('Invalid message:', data);
}
});
ws.on('close', () => {
console.log('Disconnected from the server');
});
ws.on('error', (error: Error) => {
console.error('WebSocket error:', error);
});