Skip to content

Switch DiscordRPC from C++ Social SDK to Gateway WebSocket - #28

Closed
sang765 wants to merge 3 commits into
devfrom
switch-discord-rpc-to-gateway
Closed

sang765 wants to merge 3 commits into
devfrom
switch-discord-rpc-to-gateway

Conversation

@sang765

@sang765 sang765 commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Replace Discord's proprietary C++ Social SDK (discord_partner_sdk.aar) with a direct Discord Gateway WebSocket connection using OAuth2 bearer tokens, based on the sang765/Discord-OAuth2-RPC approach.

Deleted:

  • app/libs/discord_partner_sdk.aar (proprietary AAR)
  • app/src/main/cpp/ (CMakeLists.txt, discord_social_bridge.cpp)
  • DiscordSocialNativeBridge.kt, DiscordSocialSdkAndroidInitializer.kt

Added:

  • GatewayClient.kt - OkHttp WebSocket client (identify/heartbeat/presence)
  • GatewayModels.kt, GatewayUtils.kt - gateway data structures

Modified:

  • DiscordSocialPresenceClient.kt - uses GatewayClient instead of JNI
  • DiscordPresenceManager.kt - removed native callback polling
  • DiscordRPC.kt, MainActivity.kt - signature/import cleanups
  • build.gradle.kts - removed CMake/AAR deps, added org.json
  • proguard-rules.pro - removed SDK keep rules

Replace Discord's proprietary C++ Social SDK (discord_partner_sdk.aar)
with a direct Discord Gateway WebSocket connection using OAuth2 bearer
tokens, based on the sang765/Discord-OAuth2-RPC approach.

Deleted:
- app/libs/discord_partner_sdk.aar (proprietary AAR)
- app/src/main/cpp/ (CMakeLists.txt, discord_social_bridge.cpp)
- DiscordSocialNativeBridge.kt, DiscordSocialSdkAndroidInitializer.kt

Added:
- GatewayClient.kt - OkHttp WebSocket client (identify/heartbeat/presence)
- GatewayModels.kt, GatewayUtils.kt - gateway data structures

Modified:
- DiscordSocialPresenceClient.kt - uses GatewayClient instead of JNI
- DiscordPresenceManager.kt - removed native callback polling
- DiscordRPC.kt, MainActivity.kt - signature/import cleanups
- build.gradle.kts - removed CMake/AAR deps, added org.json
- proguard-rules.pro - removed SDK keep rules
@sang765 sang765 self-assigned this Jun 5, 2026
@sang765 sang765 added the enhancement New feature or request label Jun 5, 2026
@kilo-code-bot

kilo-code-bot Bot commented Jun 5, 2026

Copy link
Copy Markdown

Code Review Summary

Status: 5 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 2
WARNING 3
Issue Details (click to expand)

CRITICAL

File Line Issue
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 35 Response object closed in onOpen callback, but response parameter in onFailure may be null causing potential NullPointerException
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 140 processingJob and heartbeatJob are cancelled but not nulled in disconnect(), inconsistent with stopHeartbeat() which nulls heartbeatJob

WARNING

File Line Issue
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 76 Response is closed in onOpen but OkHttp recommends keeping response open; closing here prevents reading response headers if needed later
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 135-140 Race condition: disconnect() sets wsSession = null asynchronously while send() checks it synchronously; caller could see inconsistent state
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 34 scope is an instance field but disconnect() launches a new coroutine using it; if disconnect() is called multiple times could cause issues
Other Observations (not in diff)
File Line Issue
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 272-284 Heartbeat loop may send heartbeats after wsSession becomes null due to timing race between if (wsSession != null) check and session closure
app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt 62 url uses encoding=json but missing compress=zlib-stream which Discord gateway recommends for compression
Files Reviewed (5 files)
  • app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayClient.kt - 5 issues
  • app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayModels.kt
  • app/src/gms/kotlin/moe/koiverse/archivetune/discord/GatewayUtils.kt
  • app/src/gms/kotlin/moe/koiverse/archivetune/discord/DiscordSocialPresenceClient.kt
  • app/src/gms/kotlin/moe/koiverse/archivetune/ui/screens/settings/DiscordPresenceManager.kt
  • app/src/gms/kotlin/moe/koiverse/archivetune/utils/DiscordRPC.kt

Reviewed by step-3.7-flash-20260528 · 37,325 tokens

Discord Gateway protocol requires image URLs to be registered via the
external-assets REST API before they can be used in presence updates.
The old C++ Social SDK handled this transparently; the Gateway approach
needs explicit registration.

- Added DiscordAssetRegistrar: caches URL-to-external_asset_path mappings,
  registers external image URLs via POST /applications/{id}/external-assets,
  and handles all image types (snowflakes, mp: prefixes, Discord CDN, raw)
- Updated DiscordSocialPresenceClient.buildPresencePayload to resolve images
  through the registrar before building the presence JSON
- Clears asset cache on client close
@sang765 sang765 closed this Jun 5, 2026
@sang765
sang765 deleted the switch-discord-rpc-to-gateway branch June 5, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant