php-fpm模式下与Swoole Websocket服务如何联系

PHP技术
486
0
0
2022-08-09

服务端

<?php
$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});
$server->on('close', function ($server, $fd) {
    echo "client {$fd} closed\n";
});
$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    global $server;//调用外部的server 
    // $server->connections 遍历所有websocket连接用户的fd,给所有用户推送 
    foreach ($server->connections as $fd) {
        // 需要先判断是否是正确的websocket连接,否则有可能会push失败 
        if ($server->isEstablished($fd)) {
            $server->push($fd, $request->get['message']);
        }
    }
    $response->end('success');
    return;
});
$server->start();

客户端连接

try {
    $client = new \GuzzleHttp\Client();
    $client->request('GET', 'http://127.0.0.1:9501', ['query' => [
        'message' => '有新品上架了',
    ]]);
} catch (Exception $e) {

}