Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websockets in variables #753

Closed
Insoleet opened this issue Jan 28, 2016 · 2 comments
Closed

Websockets in variables #753

Insoleet opened this issue Jan 28, 2016 · 2 comments
Labels

Comments

@Insoleet
Copy link
Contributor

Hello,

I'm trying to run the following :

async def connect():
    the_ws = aiohttp.ws_connect('ws://metab.ucoin.io:9201/ws/block')
    async with the_ws:

        async for msg in the_ws:
            if msg.tp == aiohttp.MsgType.text:
                print(msg.data)
            elif msg.tp == aiohttp.MsgType.closed:
                break
            elif msg.tp == aiohttp.MsgType.error:
                break

It fails with the following error :

Traceback (most recent call last):
  File "/home/inso/code/ucoin/sakia/test.py", line 22, in <module>
    loop.run_until_complete(connect())
  File "/home/inso/.pyenv/versions/sakia-env/lib/python3.5/site-packages/quamash/__init__.py", line 243, in run_until_complete
    return future.result()
  File "/home/inso/.pyenv/versions/3.5.0/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/home/inso/.pyenv/versions/3.5.0/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(value)
  File "/home/inso/code/ucoin/sakia/test.py", line 10, in connect
    async for msg in the_ws:
TypeError: 'async for' requires an object with __aiter__ method, got _DetachedWSRequestContextManager

Is it a bug or am I supposed to do it differently ?

@Insoleet
Copy link
Contributor Author

Oh, sorry, I found my mistake. The correct code should be the following :

async def connect():
    the_ws = aiohttp.ws_connect('ws://metab.ucoin.io:9201/ws/block')
    async with the_ws as ws:

        async for msg in ws:
            if msg.tp == aiohttp.MsgType.text:
                print(msg.data)
            elif msg.tp == aiohttp.MsgType.closed:
                break
            elif msg.tp == aiohttp.MsgType.error:
                break

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant