Skip to content

OpenSwoole / Swoole Websocket Transport for Thruway Router

License

Notifications You must be signed in to change notification settings

cydrickn/thruway-swoole-transport

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swoole Thruway Transport

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);
});