on_connected callback
This commit is contained in:
parent
1cccdcf99a
commit
d3be010ffd
2
chat.py
2
chat.py
|
@ -38,7 +38,7 @@ S.Endpoint(conn, S.Observe(Says(S.CAPTURE, S.CAPTURE)),
|
||||||
|
|
||||||
async def reconnect(loop):
|
async def reconnect(loop):
|
||||||
while True:
|
while True:
|
||||||
await conn.main(loop)
|
await conn.main(loop, on_connected=lambda: print('-'*50, 'Connected'))
|
||||||
if not conn: break
|
if not conn: break
|
||||||
print('-'*50, 'Disconnected')
|
print('-'*50, 'Disconnected')
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
|
|
|
@ -157,7 +157,7 @@ class TcpConnection(Connection, asyncio.Protocol):
|
||||||
if self.stop_signal:
|
if self.stop_signal:
|
||||||
self.stop_signal.set_result(True)
|
self.stop_signal.set_result(True)
|
||||||
|
|
||||||
async def main(self, loop):
|
async def main(self, loop, on_connected=None):
|
||||||
if self.transport is not None:
|
if self.transport is not None:
|
||||||
raise Exception('Cannot run connection twice!')
|
raise Exception('Cannot run connection twice!')
|
||||||
|
|
||||||
|
@ -173,6 +173,7 @@ class TcpConnection(Connection, asyncio.Protocol):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if on_connected: on_connected()
|
||||||
await self.stop_signal
|
await self.stop_signal
|
||||||
return True
|
return True
|
||||||
finally:
|
finally:
|
||||||
|
@ -196,7 +197,7 @@ class WebsocketConnection(Connection):
|
||||||
if self.ws:
|
if self.ws:
|
||||||
self.loop.call_soon_threadsafe(lambda: self.loop.create_task(self.ws.close()))
|
self.loop.call_soon_threadsafe(lambda: self.loop.create_task(self.ws.close()))
|
||||||
|
|
||||||
async def main(self, loop):
|
async def main(self, loop, on_connected=None):
|
||||||
if self.ws is not None:
|
if self.ws is not None:
|
||||||
raise Exception('Cannot run connection twice!')
|
raise Exception('Cannot run connection twice!')
|
||||||
|
|
||||||
|
@ -204,6 +205,7 @@ class WebsocketConnection(Connection):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with websockets.connect(self.url) as ws:
|
async with websockets.connect(self.url) as ws:
|
||||||
|
if on_connected: on_connected()
|
||||||
self.ws = ws
|
self.ws = ws
|
||||||
self._on_connected()
|
self._on_connected()
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue