fuyukai / asyncwebsockets Goto Github PK
View Code? Open in Web Editor NEWAsynchronous client websockets
License: MIT License
Asynchronous client websockets
License: MIT License
It seems useful to offer receive_message()
in addition to receiving by iteration. This would be in line with other async websocket API's such as aiohttp.
The use case is simple query-response sequences within a single task:
await ws.send_message('foo?')
reply = await ws.receive_message()
(using auto reconnect wouldn't make sense here)
next_event()
should probably ignore non-data messages (ping, etc.) by default to reduce surprises
When multiple tasks are sending messages you can run into BusyResourceError
as there is an await
in the send()
method.
trio-websocket
encloses the send block in a stream so perhaps this library should do the same
$ pip3 install git+https://github.com/Fuyukai/asyncwebsockets.git#egg=asyncwebsockets
Collecting asyncwebsockets from git+https://github.com/Fuyukai/asyncwebsockets.git#egg=asyncwebsockets
Cloning https://github.com/Fuyukai/asyncwebsockets.git to /private/var/folders/ww/51xbs7nx6t16x7mh225q9x1h0000gn/T/pip-build-_dz2h9_l/asyncwebsockets
Requirement already satisfied: multio in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from asyncwebsockets)
Requirement already satisfied: wsproto>=0.11.0 in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from asyncwebsockets)
Requirement already satisfied: async_generator in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from asyncwebsockets)
Requirement already satisfied: yarl in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from asyncwebsockets)
Requirement already satisfied: h11~=0.7.0 in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from wsproto>=0.11.0->asyncwebsockets)
Requirement already satisfied: multidict>=4.0 in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from yarl->asyncwebsockets)
Requirement already satisfied: idna>=2.0 in /Users/john/.pyenv/versions/3.6.5/lib/python3.6/site-packages (from yarl->asyncwebsockets)
Installing collected packages: asyncwebsockets
Running setup.py install for asyncwebsockets ... done
Successfully installed asyncwebsockets-0.2.1.dev16
You are using pip version 9.0.3, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ pydoc3 asyncwebsockets
problem in asyncwebsockets - ModuleNotFoundError: No module named 'asyncwebsockets._specific'
I noticed 0.8.1 is not uploaded to pypi. please fix. thanks.
It took me a while to find the documentation (duck duck go struggled - had to use google), so it'd be helpful to have a link to it in the readme.
When I run the example, it raises the exception:
....
File "~/lib/python3.6/site-packages/trio/_ssl.py", line 416, in _check_status
raise ClosedStreamError
trio.ClosedStreamError
Is this the expected behavior?
I'm using trio 0.3.0
import multio
from asyncwebsockets.client import connect_websocket
from asyncwebsockets.ws import WebsocketConnectionEstablished, WebsocketBytesMessage
async def test():
sock = await connect_websocket("wss://echo.websocket.org", reconnecting=False)
async for message in sock:
print("Event received", message)
if isinstance(message, WebsocketConnectionEstablished):
await sock.send_message(b"test")
elif isinstance(message, WebsocketBytesMessage):
print("Got response:", message.data)
await sock.close(code=1000, reason="Thank you!")
multio.init("trio")
multio.run(test)
The commit for the latest release is not in master: 25e58c1
Not sure what's going on. Some local branch needs pushing?
My use case involves handling Ping from a server. Currently I create the Pong payload and use internal attributes of websocket to send the reply.
# code smell as it uses internal attributes
# websocket.send assumes you want to send
# text or binary...
async def handle_server_ping(ws, item):
pong = item.response()
async with ws._send_lock:
data = ws._connection.send(pong)
await ws._sock.send(data)
If I visit "ws://127.0.0.1:9222/devtools/page/D39BF37BAFD9C1E4983F9BFF46BCEB28", the HOST should be "127.0.0.1:9222".
But now the program is set to "127.0.0.1", causing me to connect to chrome's websocket debug Interface failed.
I was looking to see if there was any websockets library available for curio.
I saw this one, but upon further digging it seems anyio 3 drops support for curio. I notice that release 0.8.2 which is the latest on pypi only requires anyio > 2 which could theoretically work, but people are likely to get an anyio > 3 release when installing anyway (as a note, this a good reason to use the ~= specifier for compatibility, which pip has supported for yonks).
So I would merely suggest dropping the language on the readme, tags, etc that it's curio-compatible, be that as it may.
I assume it's when cancel happens while waiting for ConnectionEstablished.
File "foo.py", line 50, in foo
async with open_websocket(url) as ws:
File "/.../site-packages/async_generator/_util.py", line 34, in __aenter__
return await self._agen.asend(None)
File "asyncwebsockets/asyncwebsockets/client.py", line 33, in open_websocket
await ws.close()
File "asyncwebsockets/asyncwebsockets/_specific/trio.py", line 60, in close
self._connection.close(code=code, reason=reason)
File "/.../site-packages/wsproto/connection.py", line 189, in close
self._outgoing += self._proto.close(code, reason)
AttributeError: 'NoneType' object has no attribute 'close'
Running the example in the 'Basic Usage' section of the readme raises the following exception:
AttributeError: 'Websocket' object has no attribute 'next_event'
I've seen this in two different programs now, both cases under Trio with plaintext websockets. (In one case I had an SSL server available and switching to wss worked fine.)
The behavior is that next_event()
blocks indefinitely on await receive_some()
when I know in fact the server has sent a response. Once the server subsequently sends more data (or a ping occurs), the pending data surfaces, although the newly sent data becomes stalled in the same way.
I found that lowering the receive_some()
size masks the problem.
It's reproducible, I'll try to distill an example.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.