binance-exchange / binance-java-api

binance-java-api is a lightweight Java library for the Binance API, supporting synchronous and asynchronous requests, as well as event streaming using WebSockets.
MIT License
834 stars 627 forks source link

Question: how to close the websocket connection? #431

Open LeoAlmDiniz opened 2 years ago

LeoAlmDiniz commented 2 years ago

I'm using the lib with Spring. So I used a bean to initiate the WebSocket connection.

@Configuration
public class WebSocketConfig {
    @Autowired
    OperationParametersDTO operationParametersDTO;
    @Bean
    public BinanceApiWebSocketClient binanceApiWebSocketClient() {
        BinanceApiWebSocketClient bean = BinanceApiClientFactory.newInstance().newWebSocketClient();
        bean.onCandlestickEvent(operationParametersDTO.getOperationPair().toLowerCase(),
                CandlestickInterval.ONE_MINUTE,
                new BinanceApiCallback<CandlestickEvent>() {    
                    @Override
                    public void onResponse(CandlestickEvent ce) {
                        System.out.println(ce);
                    }
                }
        );
        return bean;
    }
}

However, when I close the connection, it keeps listening to events.

`
// ... closer controller logic
binanceApiWebSocketClient.close();
binanceApiWebSocketClient = null;
//... closer controller logic
`

Am I doing something wrong here?

LeoAlmDiniz commented 2 years ago

P.S.: obviously, I'm Autowiring binanceApiWebSocketClient. System.out.println(binanceApiWebSocketClient.hashCode()) confirm it is the same object.