Improve pinging
It actually resets the ping check now. Also, I'm now using the event callbacks for sending the ping-reply, which only seems appropriate.
This commit is contained in:
parent
8c34a450c1
commit
b437731c7f
1 changed files with 18 additions and 12 deletions
|
|
@ -64,15 +64,15 @@ class Connection:
|
||||||
- "reconnecting" : No arguments
|
- "reconnecting" : No arguments
|
||||||
- "reconnected" : No arguments
|
- "reconnected" : No arguments
|
||||||
- "disconnecting" : No arguments
|
- "disconnecting" : No arguments
|
||||||
- "on_<euph event name>": the packet, parsed as JSON
|
- "<euph event name>": the packet, parsed as JSON
|
||||||
|
|
||||||
Events ending with "-ing" ("reconnecting", "disconnecting") are fired at
|
Events ending with "-ing" ("reconnecting", "disconnecting") are fired at
|
||||||
the beginning of the process they represent. Events ending with "-ed"
|
the beginning of the process they represent. Events ending with "-ed"
|
||||||
("connected", "reconnected") are fired after the process they represent has
|
("connected", "reconnected") are fired after the process they represent has
|
||||||
finished.
|
finished.
|
||||||
|
|
||||||
Examples for the last category of events include "on_message-event",
|
Examples for the last category of events include "message-event",
|
||||||
"on_part-event" and "on_ping".
|
"part-event" and "ping".
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Maximum duration between euphoria's ping messages. Euphoria usually sends
|
# Maximum duration between euphoria's ping messages. Euphoria usually sends
|
||||||
|
|
@ -114,6 +114,8 @@ class Connection:
|
||||||
self._awaiting_replies: Optional[Dict[str, asyncio.Future[Any]]] = None
|
self._awaiting_replies: Optional[Dict[str, asyncio.Future[Any]]] = None
|
||||||
self._ping_check: Optional[asyncio.Task[None]] = None
|
self._ping_check: Optional[asyncio.Task[None]] = None
|
||||||
|
|
||||||
|
self.register_event("ping-event", self._ping_pong)
|
||||||
|
|
||||||
def register_event(self,
|
def register_event(self,
|
||||||
event: str,
|
event: str,
|
||||||
callback: Callable[..., Awaitable[None]]
|
callback: Callable[..., Awaitable[None]]
|
||||||
|
|
@ -438,16 +440,13 @@ class Connection:
|
||||||
|
|
||||||
# Then, send the corresponding event
|
# Then, send the corresponding event
|
||||||
packet_type = packet["type"]
|
packet_type = packet["type"]
|
||||||
self._events.fire(f"on_{packet_type}", packet)
|
self._events.fire(packet_type, packet)
|
||||||
|
|
||||||
# Finally, if it's a ping command, reply as per
|
# Finally, reset the ping check
|
||||||
# http://api.euphoria.io/#ping
|
logger.debug("Resetting ping check")
|
||||||
if packet_type == "ping-event":
|
self._ping_check.cancel()
|
||||||
logger.debug("Pong!")
|
self._ping_check = asyncio.create_task(
|
||||||
asyncio.create_task(self._send_if_possible(
|
self._disconnect_in(self.PING_TIMEOUT))
|
||||||
"ping-reply",
|
|
||||||
{"time": packet["data"]["time"]}
|
|
||||||
))
|
|
||||||
|
|
||||||
async def _send_if_possible(self, packet_type: str, data: Any,) -> None:
|
async def _send_if_possible(self, packet_type: str, data: Any,) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -462,6 +461,13 @@ class Connection:
|
||||||
except IncorrectStateException:
|
except IncorrectStateException:
|
||||||
logger.debug("Could not send (disconnecting or already disconnected)")
|
logger.debug("Could not send (disconnecting or already disconnected)")
|
||||||
|
|
||||||
|
async def _ping_pong(self, packet):
|
||||||
|
# Implements http://api.euphoria.io/#ping and is called as "ping-event"
|
||||||
|
# callback
|
||||||
|
logger.debug("Pong!")
|
||||||
|
await self._send_if_possible("ping-reply",
|
||||||
|
{"time": packet["data"]["time"]})
|
||||||
|
|
||||||
async def send(self,
|
async def send(self,
|
||||||
packet_type: str,
|
packet_type: str,
|
||||||
data: Any,
|
data: Any,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue