Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA UserWebSocketStarter

From Leeroopedia
Knowledge Sources
Domains WebSockets, Discord_Integration
Last Updated 2026-10-13 00:00 GMT

Overview

Concrete tool for maintaining a persistent WebSocket connection to Discord's Gateway WSS endpoint with zlib compression, heartbeat lifecycle, and event dispatch, provided by the nv-websocket-client library.

Description

The UserWebSocketStarter class implements WebSocketAdapter and WebSocketStarter. Each instance represents one Discord account's WSS connection. Key responsibilities:

  • Connection: start() creates a WebSocket with zlib-stream encoding and Discord-specific headers
  • Decompression: onBinaryMessage() decompresses zlib frames, parses op-codes, routes to handlers
  • Heartbeat: handleHello() starts scheduled heartbeat at server-specified interval; reconnects if no ACK
  • Authentication: doResumeOrIdentify() sends IDENTIFY (new) or RESUME (reconnect) with account token
  • Dispatch: handleDispatch() routes READY/RESUMED to connect success, all others to UserMessageListener
  • Reconnection: tryReconnect() / tryNewConnect() with 3 retry limit; disables account on failure

Usage

Import this class when you need to:

  • Connect to Discord's Gateway WSS for real-time event monitoring
  • Understand Discord heartbeat/identify/resume lifecycle
  • Handle WSS reconnection with backoff

Code Reference

Source Location

  • Repository: GPT-WEB-JAVA
  • File: src/main/java/com/intelligent/bot/server/wss/user/UserWebSocketStarter.java

Signature

public class UserWebSocketStarter extends WebSocketAdapter implements WebSocketStarter {
    public UserWebSocketStarter(String wssServer, DiscordAccount account, UserMessageListener userMessageListener);

    public synchronized void start() throws Exception;
    public void setTrying(boolean trying);

    @Override
    public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception;
    // Internal: handleHello, doResumeOrIdentify, handleDispatch, handleHeartbeat
    // Internal: tryReconnect, tryNewConnect, tryStart
}

Import

import com.intelligent.bot.server.wss.user.UserWebSocketStarter;

I/O Contract

Inputs

Name Type Required Description
wssServer String Yes Discord Gateway WSS URL
account DiscordAccount Yes Discord account with userToken, channelId, guildId
userMessageListener UserMessageListener Yes Event dispatcher

Outputs

Name Type Description
(async) WSS events Dispatched to UserMessageListener for non-READY/RESUMED events

Usage Examples

Starting a WSS Connection

UserWebSocketStarter starter = new UserWebSocketStarter(
    "wss://gateway.discord.gg",
    account,
    userMessageListener
);
starter.start();
// Connects with zlib-stream encoding
// Sends IDENTIFY with token on HELLO
// Heartbeat scheduler started at heartbeat_interval

Op-Code Handling

// onBinaryMessage decompresses frame, parses opCode:
// op=1 (HEARTBEAT) → send heartbeat
// op=11 (HEARTBEAT_ACK) → set heartbeatAck=true
// op=10 (HELLO) → handleHello + doResumeOrIdentify
// op=0 (DISPATCH) → handleDispatch → UserMessageListener
// op=7 (RECONNECT) → sendReconnect
// op=9 (INVALIDATE_SESSION) → sendClose(1009)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment