xp-forge/websockets

适用于 XP 框架的 WebSockets

v3.1.0 2024-03-24 13:38 UTC

This package is auto-updated.

Last update: 2024-08-24 14:29:06 UTC


README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

示例

use websocket\Listeners;

class Imitator extends Listeners {

  public function serve($events) {
    return [
      '/echo' => function($conn, $payload) {
        $conn->send('You said: '.$payload);
      }
    ];
  }
}

使用以下方式运行它

$ xp -supervise ws Imitator
@peer.ServerSocket(Resource id #138 -> tcp://0.0.0.0:8081))
Serving Imitator(dev)[] > websocket.logging.ToConsole
# ...

在 JavaScript 端,按照以下方式打开连接

var ws = new WebSocket('ws://localhost:8081/echo');
ws.onmessage = (event) => console.log(event.data);

ws.send('Hello');  // Will log "You said: Hello" to the console

另请参阅