From 547b8e85b9780e749273906cf8f513114dd40ef3 Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Sun, 29 Oct 2023 14:08:46 -0700 Subject: [PATCH] changed client example to not use socket.io --- example/newClient.ts | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/example/newClient.ts b/example/newClient.ts index 913549e..5dba733 100644 --- a/example/newClient.ts +++ b/example/newClient.ts @@ -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); });