runAll
void Worker::runAll(void)
Chạy tất cả các phiên bản Worker.
Lưu ý:
Worker::runAll() thực thi và sẽ khóa vĩnh viễn, có nghĩa là mã nằm sau Worker::runAll() sẽ không được thực hiện. Tất cả các phiên bản Worker nên được khởi tạo trước Worker::runAll().
Tham số
Không có tham số
Giá trị trả về
Không có giá trị trả về
Ví dụ Chạy nhiều phiên bản Worker
start.php
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$http_worker = new Worker("http://0.0.0.0:2345");
$http_worker->onMessage = function(TcpConnection $connection, $data)
{
$connection->send('hello http');
};
$ws_worker = new Worker('websocket://0.0.0.0:4567');
$ws_worker->onMessage = function(TcpConnection $connection, $data)
{
$connection->send('hello websocket');
};
// Chạy tất cả các phiên bản Worker
Worker::runAll();
Lưu ý:
Phiên bản workerman cho windows không hỗ trợ khởi tạo nhiều Worker trong cùng một tệp.
Ví dụ trên không thể chạy trên phiên bản workerman cho windows.
Phiên bản workerman cho windows cần khởi tạo nhiều phiên bản Worker trong các tệp khác nhau, như dưới đây
start_http.php
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$http_worker = new Worker("http://0.0.0.0:2345");
$http_worker->onMessage = function(TcpConnection $connection, $data)
{
$connection->send('hello http');
};
// Chạy tất cả các phiên bản Worker (chỉ có một phiên bản ở đây)
Worker::runAll();
start_websocket.php
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$ws_worker = new Worker('websocket://0.0.0.0:4567');
$ws_worker->onMessage = function(TcpConnection $connection, $data)
{
$connection->send('hello websocket');
};
// Chạy tất cả các phiên bản Worker (chỉ có một phiên bản ở đây)
Worker::runAll();