例1

(要求Workerman版本>=3.3.0)

Workerに基づくマルチプロセス(分散クラスター)プッシュシステム、クラスターによる一斉配信、クラスターによるブロードキャスト。

start_channel.php
システム全体には1つのstart_channelサービスしかデプロイできません。仮に192.168.1.1で実行されているとします。

<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';

// Channelサーバーを初期化
$channel_server = new Channel\Server('0.0.0.0', 2206);

Worker::runAll();

start_ws.php
システム全体には複数のstart_wsサービスをデプロイできます。仮に192.168.1.2と192.168.1.3の2台のサーバーで実行されているとします。

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

// websocketサーバー
$worker = new Worker('websocket://0.0.0.0:4236');
$worker->count=2;
$worker->name = 'pusher';
$worker->onWorkerStart = function($worker)
{
    // ChannelクライアントがChannelサーバーに接続
    Channel\Client::connect('192.168.1.1', 2206);
    // 自分のプロセスIDをイベント名とする
    $event_name = $worker->id;
    // worker->idイベントに登録し、そのイベント処理関数を登録
    Channel\Client::on($event_name, function($event_data)use($worker){
        $to_connection_id = $event_data['to_connection_id'];
        $message = $event_data['content'];
        if(!isset($worker->connections[$to_connection_id]))
        {
            echo "connection not exists\n";
            return;
        }
        $to_connection = $worker->connections[$to_connection_id];
        $to_connection->send($message);
    });

    // ブロードキャストイベントを購読
    $event_name = 'ブロードキャスト';
    // ブロードキャストイベントを受け取った後、現在のプロセス内のすべてのクライアント接続にブロードキャストデータを送信
    Channel\Client::on($event_name, function($event_data)use($worker){
        $message = $event_data['content'];
        foreach($worker->connections as $connection)
        {
            $connection->send($message);
        }
    });
};

$worker->onConnect = function(TcpConnection $connection)use($worker)
{
    $msg = "workerID:{$worker->id} connectionID:{$connection->id} connected\n";
    echo $msg;
    $connection->send($msg);
};
Worker::runAll();

start_http.php
システム全体には複数のstart_httpサービスをデプロイできます。仮に192.168.1.4と192.168.1.5の2台のサーバーで実行されているとします。

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

// httpリクエストを処理し、任意のクライアントにデータをプッシュする。workerIDとconnectionIDを渡す必要があります。
$http_worker = new Worker('http://0.0.0.0:4237');
$http_worker->name = 'publisher';
$http_worker->onWorkerStart = function()
{
    Channel\Client::connect('192.168.1.1', 2206);
};
$http_worker->onMessage = function(TcpConnection $connection, $request)
{
    // Workerman 4.x 5.x と互換性
    if (!is_array($request)) {
        $get = $request->get();
    }
    $connection->send('ok');
    if(empty($get['content'])) return;
    // 特定のworkerプロセス内の特定の接続にデータをプッシュする
    if(isset($get['to_worker_id']) && isset($get['to_connection_id']))
    {
        $event_name = $get['to_worker_id'];
        $to_connection_id = $get['to_connection_id'];
        $content = $get['content'];
        Channel\Client::publish($event_name, array(
           'to_connection_id' => $to_connection_id,
           'content'          => $content
        ));
    }
    // 全体にブロードキャストデータを送信する
    else
    {
        $event_name = 'ブロードキャスト';
        $content = $get['content'];
        Channel\Client::publish($event_name, array(
           'content'          => $content
        ));
    }
};

Worker::runAll();

テスト

1、各サーバー上のサービスを実行する

2、クライアントがサーバーに接続する

Chromeブラウザを開いてF12を押してデバッグコンソールを開き、Consoleタブに次の内容を入力するか、以下のコードをHTMLページに埋め込み、JSを実行します。

// もしくはws://192.168.1.3:4236に接続できます
ws = new WebSocket("ws://192.168.1.2:4236");
ws.onmessage = function(e) {
    alert("サーバーからのメッセージ受信:" + e.data);
};

3、HTTPインターフェースを呼び出してプッシュする

URLにアクセス http://192.168.1.4:4237/?content={$content} または http://192.168.1.5:4237/?content={$content} で全てのクライアント接続に$contentデータをプッシュする

URLにアクセス http://192.168.1.4:4237/?to_worker_id={$worker_id}&to_connection_id={$connection_id}&content={$content} またはhttp://192.168.1.5:4237/?to_worker_id={$worker_id}&to_connection_id={$connection_id}&content={$content} で特定のworkerプロセス内の特定のクライアント接続に$contentデータをプッシュする

注意:テスト時には{$worker_id}{$connection_id}{$content}を実際の値に置き換えてください。