Ví dụ 1

(yêu cầu phiên bản Workerman >=3.3.0)

Hệ thống phát hành trên nhiều tiến trình (nhóm phân tán) dựa trên Worker, phát sóng nhóm, phát sóng nhóm.

start_channel.php
Toàn bộ hệ thống chỉ có thể triển khai một dịch vụ start_channel. Giả sử chạy trên 192.168.1.1.

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

// Khởi tạo một máy chủ Channel
$channel_server = new Channel\Server('0.0.0.0', 2206);

Worker::runAll();

start_ws.php
Toàn bộ hệ thống có thể triển khai nhiều dịch vụ start_ws, giả sử chạy trên 192.168.1.2 và 192.168.1.3.

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

// Máy chủ websocket
$worker = new Worker('websocket://0.0.0.0:4236');
$worker->count=2;
$worker->name = 'pusher';
$worker->onWorkerStart = function($worker)
{
    // Kết nối khách hàng Channel tới máy chủ Channel
    Channel\Client::connect('192.168.1.1', 2206);
    // Lấy id của chính tiến trình làm tên sự kiện
    $event_name = $worker->id;
    // Đăng ký sự kiện worker->id và hàm xử lý sự kiện
    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 "Kết nối không tồn tại\n";
            return;
        }
        $to_connection = $worker->connections[$to_connection_id];
        $to_connection->send($message);
    });

    // Đăng ký sự kiện phát sóng
    $event_name = 'Phát sóng';
    // Khi nhận được sự kiện phát sóng, gửi dữ liệu phát sóng tới tất cả khách hàng kết nối trong tiến trình hiện tại
    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} đã kết nối\n";
    echo $msg;
    $connection->send($msg);
};
Worker::runAll();

start_http.php
Toàn bộ hệ thống có thể triển khai nhiều dịch vụ start_http, giả sử chạy trên 192.168.1.4 và 192.168.1.5.

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

// Dùng để xử lý yêu cầu http, đẩy dữ liệu tới bất kỳ khách hàng nào, cần truyền workerID và 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)
{
    // Tương thích với workerman4.x 5.x
    if (!is_array($request)) {
        $get = $request->get();
    }
    $connection->send('ok');
    if(empty($get['content'])) return;
    // Là đẩy dữ liệu tới một kết nối trong một tiến trình 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
        ));
    }
    // Là dữ liệu phát sóng toàn cầu
    else
    {
        $event_name = 'Phát sóng';
        $content = $get['content'];
        Channel\Client::publish($event_name, array(
           'content'          => $content
        ));
    }
};

Worker::runAll();

Kiểm tra

1、Chạy dịch vụ trên từng máy chủ

2、Khách hàng kết nối với máy chủ

Mở trình duyệt chrome, nhấn F12 để mở bảng điều khiển gỡ lỗi, nhập vào phần Console (hoặc đưa đoạn mã dưới đây vào trang html để chạy bằng js)

// Cũng có thể kết nối tới ws://192.168.1.3:4236
ws = new WebSocket("ws://192.168.1.2:4236");
ws.onmessage = function(e) {
    alert("Nhận tin nhắn từ máy chủ: " + e.data);
};

3、Đẩy dữ liệu thông qua việc gọi API http

url truy cập http://192.168.1.4:4237/?content={$content} hoặc http://192.168.1.5:4237/?content={$content} để đẩy dữ liệu $content tới tất cả kết nối khách hàng

url truy cập http://192.168.1.4:4237/?to_worker_id={$worker_id}&to_connection_id={$connection_id}&content={$content} hoặchttp://192.168.1.5:4237/?to_worker_id={$worker_id}&to_connection_id={$connection_id}&content={$content} để đẩy dữ liệu $content tới một kết nối khách hàng trong một tiến trình worker

Lưu ý: Khi kiểm tra hãy thay thế {$worker_id} {$connection_id}{$content} bằng các giá trị thực tế