Open tjddnjs124 opened 3 years ago
I get the same issue. Nothing is being called back with asyncio extension. Any idea?
from binance.client import Client from binance.websockets import * from twisted.internet import reactor import threading
class my_BinanceSocketManager(BinanceSocketManager): def start_user_socket(self, callback): """Start a websocket for user data """
user_listen_key = self._client.stream_get_listen_key()
# and start the socket with this specific key
return self._start_account_socket('userData', user_listen_key, callback)
and change line: bm = BinanceSocketManager(client) to: bm = my_BinanceSocketManager(client)
@jastom1
What is the difference to already available "def start_user_socket(self, callback):"?
Please post a very simple example of usage of user_socket.
import asyncio from binance import AsyncClient, BinanceSocketManager
async def main(): client = await AsyncClient.create( "key", "secret", tld="com", # change to us if you connect to binance.us testnet=True #comment when you don't need anymore ) bm = BinanceSocketManager(client)
ts = bm.user_socket()
# then start receiving messages
async with ts as tscm:
while True:
res = await tscm.recv()
print(res)
await client.close_connection()
if name == "main":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
AttributeError: 'BinanceSocketManager' object has no attribute 'start_user_socket'
print(res)
TypeError Traceback (most recent call last)<ipython-input-46-653fd5f88a07> in <module>----> 1 async with ts as tscm: 2 res = await tscm.recv() 3 print(res) 4 5 ~/anaconda3/lib/python3.9/site-packages/binance/streams.py in __aenter__(self) 64 65 async def __aenter__(self):---> 66 await self.connect() 67 return self 68 ~/anaconda3/lib/python3.9/site-packages/binance/streams.py in connect(self) 81 82 async def connect(self):---> 83 await self._before_connect() 84 assert self._path 85 ws_url = self._url + self._prefix + self._path~/anaconda3/lib/python3.9/site-packages/binance/streams.py in _before_connect(self) 244 async def _before_connect(self):
245 if not self._path:
--> 246 self._path = await self._get_listen_key()
247
248 async def _after_connect(self):
~/anaconda3/lib/python3.9/site-packages/binance/streams.py in _get_listen_key(self)
257 async def _get_listen_key(self):
258 if self._keepalive_type == 'user':
--> 259 listen_key = await self._client.stream_get_listen_key()
260 elif self._keepalive_type == 'margin': # cross-margin
261 listen_key = await self._client.margin_stream_get_listen_key()
TypeError: object str can't be used in 'await' expression
python-binance/binance/websockets.py /
def start_user_socket(self, callback):
Get the user listen key
[My code]
import sys import time from binance.client import Client # Import the Binance Client from binance.websockets import BinanceSocketManager # Import the Binance Socket Manager
li_test1 = [] li_error = []
client = Client(api_key="secret", api_secret = "secret") bm = BinanceSocketManager(client)
def handle_message(msg): print(msg)
conn_key = bm.start_user_socket(handle_message) bm.start()
time.sleep(2) bm.stop_socket(conn_key)
" conn_key = bm.start_user_socket(handle_message) bm.start() "
1) The basic code is not work. --> how? how?
and
i think that the websocket's code is not offer futures-user-data-stream api.
2) How can i take the data that futures-user-data-stream in this codes ?
thanks, teacher.