Skip to content
This repository has been archived by the owner on Nov 23, 2017. It is now read-only.

How to keep session alive when using async websockets? #491

Open
mllamazares opened this issue Feb 9, 2017 · 1 comment
Open

How to keep session alive when using async websockets? #491

mllamazares opened this issue Feb 9, 2017 · 1 comment

Comments

@mllamazares
Copy link

mllamazares commented Feb 9, 2017

I have this code to interact with a websocket api using async and websokets python libraries:

#!/usr/bin/env python3

import sys, json
import asyncio
from websockets import connect

class AsyncWebsocket():
    async def __aenter__(self):
        self._conn = connect('wss://ws.myws.com/v2')
        self.websocket = await self._conn.__aenter__()        
        return self

    async def __aexit__(self, *args, **kwargs):
        await self._conn.__aexit__(*args, **kwargs)

    async def send(self, message):
        await self.websocket.send(message)

    async def receive(self):
        return await self.websocket.recv()

class mtest():
    def __init__(self, api_token):
        self.aws        = AsyncWebsocket()
        self.loop       = asyncio.get_event_loop()
        self.api_token  = api_token
        
        self.authorize()

    def authorize(self):
        jdata = self.__async_exec({
                                    'authorize': self.api_token
                                  })

        try:
            print (jdata['email'])
            ret = True
        except:
            ret = False

        return ret

    def sendtest(self):

        jdata = self.__async_exec({
                                    "hello": 1
                                  })

        print (jdata)


    def __async_exec(self, jdata):
        try:
            ret = json.loads(self.loop.run_until_complete(self.__async_send_recieve(jdata)))
        except:
            ret = None

        return ret

    async def __async_send_recieve(self, jdata):
        async with self.aws:
            await self.aws.send(json.dumps(jdata))
            return await self.aws.receive()

So I have the following in main.py:

from webclass import *

a = mtest('12341234')
print (a.sendtest())

The problem is that it doesn't preserve the authorized session, so this is the output:

root@ubupc1:/home/dinocob# python3 test.py 
asd@gmail.com
{'error': {'message': 'Please log in.', 'code': 'AuthorizationRequired'}}

As you see, the login call are working ok, but when calling and sending the hello in sendtest function, the session is not the same.

  • Where is destroyed the session?
  • How could I preserve it (without drastically
    modifying my class structure)?
@rutsky
Copy link

rutsky commented Feb 9, 2017

@mllamazing please submit your questions to websockets library users/developers, e.g. on https://github.com/aaugustin/websockets
Your questions are not related to (low level) asyncio library, but rather to (high level) websockets library.

Also I can recommend to try to use aiohttp for websockets.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants