WebSocket Architecture Documentation

Overview

The WebSocket system in this architecture provides real-time communication between clients. It is primarily designed to manage client-to-client controls, where a user on one device (like a phone) can control playback or the queue on another device (like a TV).

The StreamManager class is the core of this WebSocket system, responsible for managing active WebSocket connections, processing incoming messages, sending responses, and handling tasks such as updating play queues and sending commands across devices. Client connections are stored as WSS objects.


Flow of WebSocket Connections

Establishing a Connection

When a new WebSocket connection is initiated, it is handled by the AddSocket method in StreamManager. This method:

Managing WebSocket Connections

WebSocket connections are monitored for activity. The server pings each connection at intervals to ensure it is still active. If a client fails to respond after a specific duration, the connection is closed.

Ping-Pong Mechanism


Sending Commands

Once a WebSocket is connected, the HandleWebSocketAsync method listens for incoming messages. The system supports a variety of commands:


Client-to-Client Communication

One of the key features of the WebSocket system is enabling client-to-client communication. For example, a user might use their phone to control media playback on a TV. This is done by: