wsclient

package
v1.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2026 License: GPL-2.0, GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package wsclient implements the websocket listener that connects to Plex's notification channel and dispatches playing / transcode-update events into the session tracker. Reconnect, backoff, ping/pong, and read-limit policy live here.

Index

Constants

View Source
const (
	NotifTypePlaying         = "playing"
	NotifTypeTranscodeUpdate = "transcodeSession.update"
)

Plex WebSocket notification type identifiers. These match the `NotificationContainer.type` field in Plex's /:/websockets/notifications protocol and gate which handler runs for each message.

View Source
const PingInterval = 30 * time.Second

PingInterval is how often we send a Ping while the WebSocket is connected. A failed ping closes the connection so the main read loop returns, triggering a normal reconnect. This catches half-open connections (NAT/middlebox state dropped without RST).

View Source
const PongTimeout = 10 * time.Second

PongTimeout is the deadline for a single ping round-trip.

Variables

This section is empty.

Functions

func FillSessionLibrary

func FillSessionLibrary(ss *sessions.Session, media *plexapi.SessionMetadata, libs []library.Library)

FillSessionLibrary populates library labels on ss when missing, using the provided library list matched by LibrarySectionID. No-op if ss already has a library name.

func MarkTranscodePending

func MarkTranscodePending(ss *sessions.Session, transcodeSession string)

MarkTranscodePending records the transcode session key from the playing notification and flags the session as pending classification. The bare key (e.g. "pqa0kbrvowd7llgyggagzgjz") stored here is a suffix of the path-prefixed TranscodeSession.key (e.g. "/transcode/sessions/pqa0kbrvowd7llgyggagzgjz") that arrives on transcodeSession.update notifications, letting Tracker.UpdateTranscode correlate the two streams precisely even when multiple sessions are pending simultaneously. No-op if the session already has a resolved transcode kind.

func SubtitleAction

func SubtitleAction(ts *plexapi.WSTranscodeSession) string

SubtitleAction classifies a transcode session's subtitle handling. Return values are one of ValBurn, ValCopy, ValTranscode, ValNone, or "other".

func TranscodeKind

func TranscodeKind(ts *plexapi.WSTranscodeSession) string

TranscodeKind classifies a transcode session by audio/video decision and codec changes. Return values are one of ValVideo, ValAudio, ValBoth, or ValNone.

Types

type BackoffConfig

type BackoffConfig struct {
	Min time.Duration
	Max time.Duration
}

BackoffConfig holds reconnect backoff parameters. Zero values mean "use package defaults" (1s min, 30s max).

type Listener

type Listener struct {
	Client       *plex.Client
	Sessions     *sessions.Tracker
	Libraries    func() []library.Library
	RecordError  func(kind string)
	SetConnected func(bool)
	Backoff      BackoffConfig
}

Listener opens and maintains the Plex websocket notification channel, dispatching events into the session tracker. Dependencies are injected as closures and a narrow interface so the listener stays free of any import-cycle back-edge to package server; the composition root in package main wires the callbacks.

func (*Listener) ConnectAndListen

func (l *Listener) ConnectAndListen(ctx context.Context) (bool, error)

ConnectAndListen opens the WebSocket and blocks in the read loop until the connection ends. Returns (sawTraffic, err): sawTraffic is true once at least one message has been received; this is the signal the caller uses to distinguish "healthy connection dropped" from "accept-and-reject" loops.

func (*Listener) HandlePlaying

func (l *Listener) HandlePlaying(ctx context.Context, notif plexapi.WSNotification)

HandlePlaying applies a "playing"-type websocket notification to the session tracker. Stopped notifications are applied without a /status/sessions fetch (they don't need session metadata and must not be dropped when the sessions API is temporarily down); all other states require a metadata fetch to populate labels.

func (*Listener) HandleTranscodeUpdate

func (l *Listener) HandleTranscodeUpdate(notif plexapi.WSNotification)

HandleTranscodeUpdate applies a "transcodeSession.update" websocket event by correlating it with an existing session (via stored bare transcode key or media part URL) and recording the classification.

func (*Listener) Listen

func (l *Listener) Listen(ctx context.Context)

Listen runs the reconnect loop until ctx is cancelled. It maintains the canonical backoff / sawTraffic invariant: only a connection that carried traffic resets backoff; a connection that never carried a byte records a websocket_dial error and escalates.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL