Receiving streaming data after implementing asyncio websockets as a class?

前端 未结 2 943
粉色の甜心
粉色の甜心 2021-01-07 04:06

My question is closely related to the following question on Stackoverflow and the documentation here. I am defining a websockets-connection as a class. Next, I

2条回答
  •  悲&欢浪女
    2021-01-07 04:34

    I only worked with Tornado's websockets but they work pretty well and Tornado has many helpers for dealing with async code:

    import json
    import tornado
    from tornado.ioloop import PeriodicCallback
    from tornado.websocket import websocket_connect
    
    
    class EchoWebsocket:
    
        def __init__(self, url, client_id=None, client_secret=None):
            self.url = url
            self.client_id = client_id
            self.client_secret = client_secret
            self.websocket = None
    
        async def connect(self):
            if not self.websocket:
                self.websocket = await websocket_connect(self.url)
    
        async def close(self):
            await self.websocket.close()
            self.websocket = None
    
        async def read(self):
            return await self.websocket.read_message()
    
        async def write(self, message):
            await self.websocket.write_message(message)
    
    
    class DERIBIT:
    
        def __init__(self):
            self.ws = EchoWebsocket(url='wss://test.deribit.com/ws/api/v2')
            self.request = {
                "jsonrpc": "2.0",
                "method": "public/subscribe",
                "id": 42,
                "params": {
                    "channels": ["deribit_price_index.btc_usd"]}
            }
            self.callback = PeriodicCallback(self.get_ticks, 1000)
            self.callback.start()
    
        async def get_ticks(self):
            if not self.ws.websocket:
                await self.ws.connect()
            await self.ws.write(json.dumps(self.request))
            response = await self.ws.read()
            print(response)
    
    
    if __name__ == "__main__":
        deribit = DERIBIT()
        tornado.ioloop.IOLoop.current().start()
    

    Output:

    {"jsonrpc":"2.0","id":42,"result":["deribit_price_index.btc_usd"],"usIn":1587298852138977,"usOut":1587298852139023,"usDiff":46,"testnet":true}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587298851526,"price":7173.46,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587298852533,"price":7173.53,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","id":42,"result":["deribit_price_index.btc_usd"],"usIn":1587298852932540,"usOut":1587298852932580,"usDiff":40,"testnet":true}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587298852533,"price":7173.53,"index_name":"btc_usd"}}}
    

    The example above could be simplified a lot if you integrate the websocket into the DERIBIT class rather than create a separate class for it.

提交回复
热议问题