|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# MIT License |
|
3
|
|
|
# |
|
4
|
|
|
# Copyright (c) 2021 Pyscord |
|
5
|
|
|
# |
|
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
# in the Software without restriction, including without limitation the rights |
|
9
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
# furnished to do so, subject to the following conditions: |
|
12
|
|
|
# |
|
13
|
|
|
# The above copyright notice and this permission notice shall be included in all |
|
14
|
|
|
# copies or substantial portions of the Software. |
|
15
|
|
|
# |
|
16
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22
|
|
|
# SOFTWARE. |
|
23
|
|
|
|
|
24
|
|
|
from __future__ import annotations |
|
25
|
|
|
|
|
26
|
|
|
from asyncio import sleep |
|
27
|
|
|
from typing import Optional |
|
28
|
|
|
|
|
29
|
|
|
from websockets.legacy.client import WebSocketClientProtocol |
|
30
|
|
|
|
|
31
|
|
|
from pyscord.core.dispatch import GatewayDispatch |
|
32
|
|
|
|
|
33
|
|
|
heartbeat: float = 0 |
|
34
|
|
|
sequence: Optional[int] = None |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
async def __send_heartbeat(socket: WebSocketClientProtocol): |
|
38
|
|
|
# TODO: Fix docs |
|
39
|
|
|
# TODO: Implement logging |
|
40
|
|
|
await socket.send(str(GatewayDispatch(1, sequence))) |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
async def handle_hello(socket: WebSocketClientProtocol, |
|
44
|
|
|
payload: GatewayDispatch): |
|
45
|
|
|
# TODO: Fix docs |
|
46
|
|
|
# TODO: Implement logging |
|
47
|
|
|
global heartbeat, sequence |
|
48
|
|
|
heartbeat = payload.data.get("heartbeat_interval") |
|
49
|
|
|
|
|
50
|
|
|
if not heartbeat: |
|
51
|
|
|
# TODO: Throw invalid heartbeat exception if no heartbeat is present. |
|
52
|
|
|
return |
|
53
|
|
|
|
|
54
|
|
|
heartbeat = heartbeat / 1000 |
|
55
|
|
|
|
|
56
|
|
|
await __send_heartbeat(socket) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
async def handle_heartbeat(socket: WebSocketClientProtocol, _): |
|
60
|
|
|
# TODO: Fix docs |
|
61
|
|
|
# TODO: Implement logging |
|
62
|
|
|
await sleep(heartbeat) |
|
63
|
|
|
await __send_heartbeat(socket) |
|
64
|
|
|
|