Skip to main content

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.

Usage

For a reference implementation see wumpy-gateway. It is designed to handle all sort of network failures and race conditions.

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 discord_gateway import DiscordConnection


TOKEN = 'ABC123.XYZ789'
RECV_SIZE = 65536
SERVER_NAME = 'gateway.discord.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.discord.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': 'discord-gateway',
                '$device': 'discord-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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discord-gateway-0.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

discord_gateway-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file discord-gateway-0.1.0.tar.gz.

File metadata

  • Download URL: discord-gateway-0.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for discord-gateway-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f2b9f5a1a85090a3ed3278ec3601e8b61b7d7cfd3316e6cfde9abb3898109152
MD5 2f599ed62df094ea16c25dfd84f5a8aa
BLAKE2b-256 a26d5e0718a5dfe344b732bbc6d5c8b55800f4bcd32e7b460cf7384a509e6012

See more details on using hashes here.

File details

Details for the file discord_gateway-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_gateway-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf33995d32ee3db3e5bcb52eb0a3a86c8a27cf6baa4e190ce5bffff10a5b2e94
MD5 8aef9b3562eed1c6e7fb501541627ca2
BLAKE2b-256 1353b81e12c7cff319b0fbd5226bb3760de29405e349ae931a79a35948ff4d93

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page