python-binance
python-binance copied to clipboard
Asyncio start_multiplex_socket no callback
Describe the bug When I try to start the socket connection to stream the klines nothing occurs. Callback isn't fired. I do get an initial response "streams=eosbtc@kline_1h/linkbtc@kline_1h/ethbtc@kline_1h" However, nothing happens after that.
To Reproduce
config = {
"pair_list": [
"EOSBTC",
"LINKBTC",
"ETHBTC"
],
"interval": '1h'
}
filename = "credentials.txt"
trader = Trader(filename)
result = await trader.connect()
if result:
trading_bot = TradingBot(trader, config)
try:
await trading_bot.init()
finally:
await trading_bot.start_websocket()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
#################Bottom is in a separate file##########################
# Initializes websocket stream
async def start_websocket(self):
# Clean and prepare list of pairs for socket stream
crypto = [f"{trading_pair.lower()}@kline_{self.interval}" for trading_pair in self.pair_list]
# Binance Socket Manager
trader = BinanceSocketManager(self.trader, asyncio.get_event_loop())
# Streams pairs sent in list
# Contains callback to handle data after each request
result = await trader.start_multiplex_socket(crypto, self.socket_manager)
print('result', result)```
Any any advice/suggestions or sort of help would be appreciated
Hi I get the same issue, curious to hear what the problem is.
I ended up rebuilding some of his code. Something in the code was broken.
` import asyncio import websockets import json
class WebSocketServer():
STREAM_URL = 'wss://stream.binance.com:9443/'
MAX_RECONNECTS = 5
MAX_RECONNECT_SECONDS = 60
MIN_RECONNECT_WAIT = 0.1
TIMEOUT = 5
def __init__(self, path, callback, prefix='ws/'):
self.socket = None
self.path = path
self.prefix = prefix
self.callback = callback
self.connect()
def connect(self):
asyncio.ensure_future(self.consume())
async def consumer_handler(self, websocket):
async for message in websocket:
self.callback(json.loads(message))
async def consume(self):
websocket_resource_url = f"{self.STREAM_URL}{self.prefix}{self.path}"
async with websockets.connect(websocket_resource_url) as websocket:
self.socket = websocket
await self.consumer_handler(websocket)
class WebSocketManager(): WEBSOCKET_DEPTH_5 = '5' WEBSOCKET_DEPTH_10 = '10' WEBSOCKET_DEPTH_20 = '20'
user_timeout = 30 * 60 # 30 minutes
def __init__(self, client, loop):
self.connections = {}
self.user_timer = None
self.user_listen_key = None
self.user_callback = None
self.client = client
self.loop = loop
async def start_socket(self, path, callback, prefix):
if path in self.connections:
return False
self.connections[path] = WebSocketServer(path, callback, prefix)
return path
async def start_multiplex_socket(self, streams, callback):
path = f"streams={'/'.join(streams)}"
await self.start_socket(path, callback, 'stream?')
return path
`
Never got around to finishing up the bot but it was close. just lost interest
same issue for me, maybe make a pull request with your fix?