cydrickn/thruway-swoole-transport

Swoole Transport for Thruway

v1.1.1 2023-11-06 11:27 UTC

This package is auto-updated.

Last update: 2024-11-06 13:37:54 UTC


README

OpenSwoole / Swoole Websocket Transport for Thruway Router

Prerequisite:

Installation

composer require cydrickn/thruway-swoole-transport

Example

Ouside the worker start

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Thruway\SwooleTransport\Server\WebsocketServer;
use Thruway\SwooleTransport\SwooleLoop;
use Thruway\SwooleTransport\SwooleTransportProvider;
use Thruway\Peer\Router;

$server = new WebsocketServer('127.0.0.1', 9999);
$transportProvider = new SwooleTransportProvider($server);

$router = new Router(new SwooleLoop());
$router->addTransportProvider($transportProvider);

$server->addListener(WebsocketServer::EVENT_SERVER_START, function () use($transportProvider, $server) {
    \Thruway\Logging\Logger::info($transportProvider, 'Websocket listening on 0.0.0.0:9000');
    $server->shutdown();
});

$router->start(false);

Inside Worker Start

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Thruway\SwooleTransport\Server\WebsocketServer;
use Thruway\SwooleTransport\SwooleLoop;
use Thruway\SwooleTransport\SwooleTransportProvider;
use Thruway\Peer\Router;

$server = new WebsocketServer('127.0.0.1', 9999);

$server->addListener(WebsocketServer::EVENT_SERVER_START, function () {
    \Thruway\Logging\Logger::info(null, 'Websocket listening on 0.0.0.0:9000');
});

$server->addListener(WebsocketServer::EVENT_WORKER_START, function () use($server) {
    $transportProvider = new SwooleTransportProvider($server);

    $router = new Router(new SwooleLoop());
    $router->addTransportProvider($transportProvider);

    $router->start(false);
});