Principle:A616567126 GPT WEB JAVA Discord WSS Communication
| Knowledge Sources | |
|---|---|
| Domains | Real_Time_Communication, WebSockets |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Communication pattern that maintains a persistent WebSocket connection to Discord's Gateway, using zlib compression, heartbeat/ack lifecycle, and op-code dispatch to receive real-time events.
Description
Discord's Gateway is a WebSocket Secure (WSS) endpoint that streams events (messages, interactions, presence) in real time. Unlike simple WebSocket usage, Discord requires zlib stream compression, heartbeat/ack keep-alive, identify/resume authentication, and event-type dispatch routing.
This repository connects to Discord's Gateway as a bot user to monitor Midjourney task results. Each Discord account maintains its own WSS connection via UserWebSocketStarter. When a Midjourney interaction completes, Discord sends a MESSAGE_CREATE or MESSAGE_UPDATE event, which is dispatched to type-specific handlers (ImagineSuccess, UpscaleSuccess, etc.).
Usage
Use this principle when:
- Integrating with Discord's bot API for real-time event monitoring
- Building WSS clients that require compression and heartbeat management
- Receiving asynchronous results from long-running interactions
Theoretical Basis
Gateway Lifecycle:
1. Client → WSS Connect (zlib-stream encoding)
2. Server → op=10 HELLO (heartbeat_interval)
3. Client → op=1 HEARTBEAT (every interval)
4. Server → op=11 HEARTBEAT_ACK
5. Client → op=2 IDENTIFY (token, properties, presence)
6. Server → op=0 DISPATCH t=READY (session_id, resume_gateway_url)
7. Server → op=0 DISPATCH t=MESSAGE_CREATE (event data)
8. Client → handle dispatch via MessageListener
Key Properties:
- Zlib compression: Binary frames are decompressed via
ZlibDecompressorbefore JSON parsing - Heartbeat: Must send
op=1at the interval specified in HELLO; if no ACK before next interval, reconnect - Identify/Resume: First connection sends IDENTIFY; reconnection sends RESUME with session_id and sequence
- Sequence tracking: Each dispatch event includes a sequence number
sfor resume support - Op-code dispatch:
op=0events are routed bytfield (READY, MESSAGE_CREATE, etc.)
Practical Guide
The repository implements this in UserWebSocketStarter:
start()creates a WebSocket connection with zlib-stream encoding and Discord-specific headersonBinaryMessage()decompresses frames, parses op-codes, and routes to handlershandleHello()starts the heartbeat scheduler at the server-specified intervaldoResumeOrIdentify()sends IDENTIFY (new) or RESUME (reconnect) based on session statehandleDispatch()extracts the event type and delegates toUserMessageListener.onMessage()UserMessageListenerfilters by channel_id and dispatches to registeredMessageHandlerimplementations
Related Pages
Implemented By
- Implementation:A616567126_GPT_WEB_JAVA_UserWebSocketStarter
- Implementation:A616567126_GPT_WEB_JAVA_UserMessageListener
- Implementation:A616567126_GPT_WEB_JAVA_AsyncLockUtils
- Implementation:A616567126_GPT_WEB_JAVA_DiscordAccountConfigController
- Implementation:A616567126_GPT_WEB_JAVA_DiscordAccountConfigServiceImpl