Mohammad Namakshenas
Mohammad Namakshenas

Reputation: 85

Hub Connection is not invoking method to server using SignalR in Python

I'm trying to establish a hub connection and negotiate through an API provided by a stock exchange company. This is the piece of code developed using the Signalr_aio in Python.

from signalr_aio import Connection
import asyncio
from requests import Session

async def pushMessage(**msg):
    print(msg)
    if 'R' in msg and type(msg['R']) is not bool:
        token = msg['R']
        sessionRealtime.headers.update({'Authorization': 'Bearer {}'.format(token)})

server_url = 'https://edbi.ephoenix.ir/realtime'
sessionRealtime = Session()
connection = Connection(server_url, session=sessionRealtime)
connection.received += pushMessage
hub = connection.register_hub('omsclienttokenhub')
hub.server.invoke('GetAPIToken', 'xxxxxxxxx', 'yyyyyyyyyy')
hub = connection.register_hub('omsclienthub')
connection.start()

I tried the 'GetTime' method and it did successfully returned the time,

hub.server.invoke('GetTime')

{'R': '00:19:10', 'I': '2'}

However, I get an error when I invoke the 'GetInstrumentList' method,

hub.server.invoke('GetInstrumentList')

{'R': {'ex': {'i': None, 'm': 'Object reference not set to an instance of an object.'}}, 'I': '1'}

I guess that there is a problem in updating the header of the request? Or should I transport the token as a querystring in a modified URL?!

Upvotes: 0

Views: 922

Answers (1)

Nandan Rana
Nandan Rana

Reputation: 539

Try out below code

from requests import Session
from signalr import Connection
import asyncio

server_url = 'https://edbi.ephoenix.ir/realtime'

async def pushMessage(**msg):
    print(msg)
    if 'R' in msg and type(msg['R']) is not bool:
        token = msg['R']
        global server_url
        server_url = 'https://edbi.ephoenix.ir/realtime' +  "?Token={}".format(token)

with Session() as session:
    #create a connection  
    connection = Connection(server_url, session)
    connection.received += pushMessage
    hub = connection.register_hub('omsclienttokenhub')
    hub.server.invoke('GetAPIToken', 'xxxxxxxxx', 'yyyyyyyyyy')
    hub = connection.register_hub('omsclienthub')
    connection.start()

I had taken some reference from this link

Upvotes: 1

Related Questions