xp-forge / websockets
适用于 XP 框架的 WebSockets
v3.1.0
2024-03-24 13:38 UTC
Requires
- php: >=7.0.0
- xp-forge/uri: ^2.0 | ^1.4
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
- xp-framework/logging: ^11.0 | ^10.0 | ^9.1
- xp-framework/networking: ^10.0 | ^9.3
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
示例
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