|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { ChangeEvent, useEffect, useState } from 'react'; |
| 4 | + |
| 5 | +import { socketClient as ws } from '../lib/api/bybit-clients'; |
| 6 | + |
| 7 | + |
| 8 | +ws.on('update', (data) => { |
| 9 | + console.log('data received', data); |
| 10 | +}); |
| 11 | + |
| 12 | +ws.on('open', (data) => { |
| 13 | + console.log('connection opened open:', data.wsKey); |
| 14 | +}); |
| 15 | +ws.on('response', (data) => { |
| 16 | + console.log('log response: ', JSON.stringify(data, null, 2)); |
| 17 | +}); |
| 18 | +ws.on('reconnect', ({ wsKey }) => { |
| 19 | + console.log('ws automatically reconnecting.... ', wsKey); |
| 20 | +}); |
| 21 | +ws.on('reconnected', (data) => { |
| 22 | + console.log('ws has reconnected ', data?.wsKey); |
| 23 | +}); |
| 24 | + |
| 25 | +ws.on('exception', (data) => { |
| 26 | + console.error('ws exception: ', data); |
| 27 | +}); |
| 28 | + |
| 29 | +export default function OptionSelector() { |
| 30 | + const [selectedOption, setSelectedOption] = useState(''); |
| 31 | + |
| 32 | + const handleValueChange = (e: ChangeEvent<HTMLSelectElement>) => { |
| 33 | + e.preventDefault(); |
| 34 | + const value = e.target.value; |
| 35 | + if (value) { |
| 36 | + if (selectedOption && selectedOption !== value) { |
| 37 | + ws.unsubscribeV5(`tickers.${selectedOption}`, 'linear'); |
| 38 | + } |
| 39 | + |
| 40 | + ws.subscribeV5(`tickers.${value}`, 'linear'); |
| 41 | + |
| 42 | + } else { |
| 43 | + if (selectedOption) { |
| 44 | + ws.unsubscribeV5(`ticker.${selectedOption}`, 'linear'); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + setSelectedOption(value); |
| 49 | + }; |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + console.log(selectedOption); |
| 53 | + }, [selectedOption]); |
| 54 | + |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <select |
| 58 | + onChange={handleValueChange} |
| 59 | + value={selectedOption} |
| 60 | + name="option-selector" |
| 61 | + > |
| 62 | + <option value="">Select an option</option> |
| 63 | + <option value="BTCUSDT">BTCUSDT</option> |
| 64 | + <option value="ETHUSDT">ETHUSDT</option> |
| 65 | + <option value="SOLUSDT">SOLUSDT</option> |
| 66 | + </select> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +} |
0 commit comments