slazarov / python-signalr-client

SignalR client for python based on asyncio.
MIT License
33 stars 22 forks source link

Cryptopia websocket error #7

Closed Wyctus closed 6 years ago

Wyctus commented 6 years ago

My problem is that I'm unable to use the library with Cryptopia's websocket. Cryptopia's websocket is a totally undocumented, non-public websocket. It also uses signalR. The endpoint url is www.cryptopia.co.nz/signalr and the hub is called 'notificationHub'.

I just tried to modify the example code:

from base64 import b64decode
from zlib import decompress, MAX_WBITS
import json

async def process_message(message):
    deflated_msg = decompress(b64decode(message), -MAX_WBITS)
    return json.loads(deflated_msg.decode())

# Create debug message handler.
async def on_debug(**msg):
    # In case of 'queryExchangeState'
    if 'R' in msg and type(msg['R']) is not bool:
        decoded_msg = await process_message(msg['R'])
        print(decoded_msg)

# Create error handler
async def on_error(msg):
    print(msg)

# Create hub message handler
async def on_message(msg):
    decoded_msg = await process_message(msg[0])
    print(decoded_msg)

if __name__ == "__main__":
    # Create connection
    # Users can optionally pass a session object to the client, e.g a cfscrape session to bypass cloudflare.
    connection = Connection('https://www.cryptopia.co.nz/signalr', session=None)

    # Register hub
    hub = connection.register_hub('notificationHub')

    # Assign debug message handler. It streams unfiltered data, uncomment it to test.
    connection.received += on_debug

    # Assign error handler
    connection.error += on_error

    # Assign hub message handler
    hub.client.on('uE', on_message)
    hub.client.on('uS', on_message)

    # Send a message
    hub.server.invoke('setTradePairSubscription', 1, 2)

    # Start the client
    connection.start()

But I always get the following error:

File "some_path\signalr_aio\hubs_hub.py", line 43, in handle

await self.__handlers[method](message)
KeyError: 'SendTradeDataUpdate'

What's the problem?

slazarov commented 6 years ago

Hi, the example code you are using is customized for Bittrex, i.e def process_message might not be applicable to Cryptopia. Secondly the message handlers “uE” and “uS” are again Bittrex specific. In your case you should omit them and add “sendtradedataupdate”: hub.client.on(“SendTradeDataUpdate”, on_message)

Wyctus commented 6 years ago

Thank you, it works now. How did you find out that the event name is 'SendTradeDataUpdate'? It seems a little bit strange, that the response is the same whether I use the on() method or not. Other strange thing is, that this returns only prices and can't find the orderbook stream. But this isn't closely related to your library.

slazarov commented 6 years ago

Hi, I figured it out from your error message: KeyError: 'SendTradeDataUpdate'

You use the on() method to register your message handler. I didn't understand completely what you meant by whether you use it or not.

About the output you are getting, I can't really comment on that since I am not familiar with Cryptopia's invokes and handlers.

Wyctus commented 6 years ago

Great, thank you!

arnaldojvg commented 6 years ago

@Wyctus could you share the working code??