11import logging
22
3- import functools
43import asyncio
5- import aiohttp
6- import aiohttp .web
74
8- from .messages .envelope import FramedBuffer
5+ from .. messages .envelope import FramedBuffer
96
107logger = logging .getLogger (__name__ )
118
@@ -28,25 +25,6 @@ async def send(self, bytes_):
2825 raise NotImplementedError ("subclasses should implement this" )
2926
3027
31- class WebSocket (Transport ):
32- def __init__ (self , ws ):
33- self .ws = ws
34-
35- async def __anext__ (self ):
36- msg = await self .ws .__anext__ ()
37- return msg .data
38-
39- async def close (self ):
40- return await self .ws .close ()
41-
42- @property
43- def closed (self ):
44- return self .ws .closed
45-
46- async def send (self , bytes_ ):
47- await self .ws .send_bytes (bytes_ )
48-
49-
5028async def watch_queue (conn , buf ):
5129 while not conn .closed :
5230 try :
@@ -145,35 +123,3 @@ async def server(self, transport):
145123 await self .start_processing ()
146124 finally :
147125 self .unregister ()
148-
149-
150- async def connect (uri , factory , loop = None ):
151- if not loop :
152- loop = asyncio .get_event_loop ()
153-
154- worker = factory ()
155- try :
156- async with aiohttp .ClientSession ().ws_connect (uri ) as ws :
157- t = WebSocket (ws )
158- await worker .client (t )
159- except Exception :
160- logger .exception ("connect" )
161- finally :
162- await asyncio .sleep (5 )
163- logger .debug ("reconnecting" )
164- loop .create_task (connect (uri , factory = factory , loop = loop ))
165-
166-
167- async def serve (request , factory ):
168- ws = aiohttp .web .WebSocketResponse ()
169- await ws .prepare (request )
170-
171- t = WebSocket (ws )
172- await factory ().server (t )
173-
174-
175- def app (factory ):
176- handler = functools .partial (serve , factory = factory )
177- app = aiohttp .web .Application ()
178- app .add_routes ([aiohttp .web .get ("/" , handler )])
179- return app
0 commit comments