Sans-I/O implementation of the Discord gateway
Project description
Discord gateway
Sans-I/O Python implementation of the Discord gateway.
Sans-I/O means that this implements no I/O (network) and operates purely on the
bytes given using wsproto
.
It means that this implementation can be reused for libraries implemented in a threading fashion or asyncio/trio/curio.
Quickstart
Here's a very minimal implementation using the socket
library and threading
for the heartbeating (it does not handle reconnecting or any form of
unexpected disconnections):
import socket
import ssl
import threading
import time
from sys import platform
import certifi
from ez_gateway import DiscordConnection
TOKEN = 'ABC123.XYZ789'
RECV_SIZE = 65536
SERVER_NAME = 'gateway.ez.gg'
def heartbeat(conn, sock):
while True:
sock.send(conn.heartbeat())
time.sleep(conn.heartbeat_interval)
def recv_event(conn, sock):
while True:
for event in conn.events():
return event
for to_send in conn.receive(sock.recv(RECV_SIZE)):
sock.send(to_send)
def main():
# Setup the socket and SSL for the WebSocket Secure connection.
conn = DiscordConnection('gateway.ez.gg', encoding='json')
ctx = ssl.create_default_context(cafile=certifi.where())
sock = socket.create_connection(conn.destination)
sock = ctx.wrap_socket(sock, server_hostname=SERVER_NAME)
sock.send(conn.connect()) # Convert to a WebSocket
# Receive the very first HELLO event.
hello = recv_event(conn, sock)
# Send RESUME or IDENTIFY depending on state (will always be False
# when initially connecting, but may be different when reconnecting).
if conn.should_resume:
sock.send(conn.resume(TOKEN))
else:
sock.send(conn.identify(
token=TOKEN,
intents=65535,
properties={
'$os': platform,
'$browser': 'ez-gateway',
'$device': 'ez-gateway'
},
))
heartbeater = threading.Thread(target=heartbeat, args=(conn,sock))
heartbeater.start()
try:
while True:
event = recv_event(conn, sock)
print('Received:', event)
finally:
sock.shutdown(socket.SHUT_WR)
sock.close()
if __name__ == '__main__':
main()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ez_gateway-0.1.0.tar.gz
(9.5 kB
view details)
Built Distribution
File details
Details for the file ez_gateway-0.1.0.tar.gz
.
File metadata
- Download URL: ez_gateway-0.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d69027a9458ced9ea76903b2eeb29ed6d7e24d1f51abe451080a56aefaec563b |
|
MD5 | df3b587a25405ae400ed1e2995f242f4 |
|
BLAKE2b-256 | 8196586ea928ff92e8558885758f5809048cc81b8c093a61527095f536979256 |
File details
Details for the file ez_gateway-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: ez_gateway-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9be47ac4627ccb170a64f7fe6288fae18af91fc2e55cb7761975c0265d93f9f1 |
|
MD5 | cf8f1d19747048f3c06952beb2df81db |
|
BLAKE2b-256 | aae95d8f66cb8ab9f19597d1f864f31b2c25789f4a9ac5cfc938dac209958dcc |