Skip to content

SP1: Boot WebSocket worker on :8097 with shared SyncPlayManager#338

Merged
detain merged 1 commit into
masterfrom
fix/server-sp1-syncplay-ws-boot
Jun 29, 2026
Merged

SP1: Boot WebSocket worker on :8097 with shared SyncPlayManager#338
detain merged 1 commit into
masterfrom
fix/server-sp1-syncplay-ws-boot

Conversation

@detain

@detain detain commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Summary

Implements SP1 of Phase 8 — Boot the WebSocket worker on :8097 with a shared, initialized SyncPlayManager.

Changes

  • config/server.php: Add websocket config block with host, port, stale_connection_timeout, and stale_group_timeout settings

  • src/Server/WebSocket/WebSocketServer.php:

    • Accept injected MessageHandler in constructor (for SP1 singletons)
    • Add SyncPlayManager property and setSyncPlayManager() method
    • Update onStart() to include cleanupStaleGroups() timer alongside existing stale-connection cleanup
  • start.php: Add WS worker (count=1, name='phlix-server-ws') before Worker::runAll():

    • Applies curated coroutine hooks
    • Builds container, creates one SyncPlayManager
    • Calls $manager->initialize($messageHandler) to bind per-type callbacks
    • Wires WebSocketServer to shared manager + MessageHandler/ConnectionPool singletons
  • tests/Unit/Server/WebSocket/WebSocketServerTest.php: New test file verifying WS worker construction and SyncPlayManager initialization

Success Conditions (per SP1 spec)

  • php start.php start brings up phlix-server-ws worker listening on :8097
  • Raw WS client receives the connected welcome
  • One SyncPlayManager instance handles all connections (logged once)
  • Existing HTTP workers (count=14) remain unaffected

Gate

  • PHPUnit: 129 tests, 314 assertions ✓
  • PHPStan level 9: No errors ✓
  • PHPCS PSR12: No errors (pre-existing start.php warning not introduced by this PR) ✓

Notes

  • Frame-shape (flat vs nested envelope) is handled in SP2
  • REST endpoint consistency is handled in SP5

- Add websocket config block to config/server.php with host/port/timeouts
- Modify WebSocketServer to accept injected MessageHandler (for SP1 singletons)
- Add setSyncPlayManager() method and stale group cleanup timer in onStart()
- Add WS worker (count=1, name=phlix-server-ws) in start.php before Worker::runAll()
  - Applies curated coroutine hooks
  - Builds container and creates one SyncPlayManager
  - Initializes manager with MessageHandler for per-type callbacks
  - Wires WebSocketServer with manager + ConnectionPool/MessageHandler singletons
  - Keeps stale-connection cleanup; adds cleanupStaleGroups() timer
- Add unit tests for WebSocketServer construction and SyncPlayManager init

Success conditions:
- php start.php start brings up phlix-server-ws worker on :8097
- Raw WS client receives the 'connected' welcome
- One SyncPlayManager instance handles all WS connections
- HTTP workers (count=14) remain unaffected
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 5 medium · 35 minor

Alerts:
⚠ 40 issues (≤ 0 issues of at least minor severity)

Results:
40 new issues

Category Results
BestPractice 3 medium
Documentation 19 minor
CodeStyle 16 minor
Complexity 2 medium

View in Codacy

🟢 Metrics 34 complexity · 0 duplication

Metric Results
Complexity 34
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9c48a2f97b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread start.php
$wsConfig['stale_group_timeout'] = $wsConfig['stale_group_timeout'] ?? 3600;

/** @var array<string, mixed> $wsConfig */
$wsServer = new \Phlix\Server\WebSocket\WebSocketServer($wsConfig, $messageHandler);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Attach the callbacks to the listening WebSocket worker

When the daemon is started with php start.php start, the worker created at new Worker('websocket://0.0.0.0:8097') is the one Workerman forks and uses to accept client connections. This line constructs a separate WebSocketServer inside onWorkerStart, which creates its own private Worker after Worker::runAll() has already selected the workers, so its onConnect/onMessage callbacks are not installed on the listening $w. As a result, clients connecting to port 8097 will not receive the connected welcome or have messages routed to MessageHandler/SyncPlayManager; bind the callbacks to $w or create the WebSocketServer worker before runAll() instead.

Useful? React with 👍 / 👎.

Comment thread start.php
// -----------------------------------------------------------------------------

try {
$wsWorker = new Worker('websocket://0.0.0.0:8097');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the configured WebSocket listen address

The new config/server.php block exposes websocket.host and websocket.port, but the actual Workerman listener is still hard-coded to 0.0.0.0:8097. In any deployment that overrides the config, the code below may log/construct the inner server with the configured values while the socket that clients actually reach still binds to all interfaces on 8097, so the configured WebSocket endpoint is ignored. Build this listen URI from $config['websocket'] before creating $wsWorker.

Useful? React with 👍 / 👎.

@detain detain merged commit fbfccf9 into master Jun 29, 2026
11 of 14 checks passed
@detain detain deleted the fix/server-sp1-syncplay-ws-boot branch June 29, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant