PHP code example of fage1151 / swoole-worker

1. Go to this page and download the library: Download fage1151/swoole-worker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fage1151 / swoole-worker example snippets



orkerman\Worker;

// Create a Websocket server
$ws_worker = new Worker("websocket://0.0.0.0:2346");

// 4 processes
$ws_worker->count = 4;

// Emitted when new connection come
$ws_worker->onConnect = function($connection)
{
    echo "New connection\n";
 };

// Emitted when data received
$ws_worker->onMessage = function($connection, $data)
{
    // Send hello $data
    $connection->send('hello ' . $data);
};

// Emitted when connection closed
$ws_worker->onClose = function($connection)
{
    echo "Connection closed\n";
};

// Run worker
Worker::runAll();


orkerman\Worker;

// #### http worker ####
$http_worker = new Worker("http://0.0.0.0:2345");

// 4 processes
$http_worker->count = 4;

// Emitted when data received
$http_worker->onMessage = function($connection, $data)
{
    // $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES are available
    var_dump($_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES);
    // send data to client
    $connection->send("hello world \n");
};

// run all workers
Worker::runAll();


orkerman\WebServer;
use Workerman\Worker;

// WebServer
$web = new WebServer("http://0.0.0.0:80");

// 4 processes
$web->count = 4;

// Set the root of domains
$web->addRoot('www.your_domain.com', '/your/path/Web');
$web->addRoot('www.another_domain.com', '/another/path/Web');
// run all workers
Worker::runAll();


orkerman\Worker;

// #### create socket and listen 1234 port ####
$tcp_worker = new Worker("tcp://0.0.0.0:1234");

// 4 processes
$tcp_worker->count = 4;

// Emitted when new connection come
$tcp_worker->onConnect = function($connection)
{
    echo "New Connection\n";
};

// Emitted when data received
$tcp_worker->onMessage = function($connection, $data)
{
    // send data to client
    $connection->send("hello $data \n");
};

// Emitted when new connection come
$tcp_worker->onClose = function($connection)
{
    echo "Connection closed\n";
};

Worker::runAll();


orkerman\Worker;

// SSL context.
$context = array(
    'ssl' => array(
        'local_cert' => '/your/path/of/server.pem',
        'local_pk'   => '/your/path/of/server.key',
    )
);

// Create a Websocket server with ssl context.
$ws_worker = new Worker("websocket://0.0.0.0:2346", $context);

// Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://). 
// The similar approaches for Https etc.
$ws_worker->transport = 'ssl';

$ws_worker->onMessage = function($connection, $data)
{
    // Send hello $data
    $connection->send('hello ' . $data);
};

Worker::runAll();


namespace Protocols;
/**
 * User defined protocol
 * Format Text+"\n"
 */
class MyTextProtocol
{
    public static function input($recv_buffer)
    {
        // Find the position of the first occurrence of "\n"
        $pos = strpos($recv_buffer, "\n");
        // Not a complete package. Return 0 because the length of package can not be calculated
        if($pos === false)
        {
            return 0;
        }
        // Return length of the package
        return $pos+1;
    }

    public static function decode($recv_buffer)
    {
        return trim($recv_buffer);
    }

    public static function encode($data)
    {
        return $data."\n";
    }
}


orkerman\Worker;

// #### MyTextProtocol worker ####
$text_worker = new Worker("MyTextProtocol://0.0.0.0:5678");

$text_worker->onConnect = function($connection)
{
    echo "New connection\n";
};

$text_worker->onMessage =  function($connection, $data)
{
    // send data to client
    $connection->send("hello world \n");
};

$text_worker->onClose = function($connection)
{
    echo "Connection closed\n";
};

// run all workers
Worker::runAll();


orkerman\Worker;
use Workerman\Lib\Timer;

$task = new Worker();
$task->onWorkerStart = function($task)
{
    // 2.5 seconds
    $time_interval = 2.5; 
    $timer_id = Timer::add($time_interval, 
        function()
        {
            echo "Timer run\n";
        }
    );
};

// run all workers
Worker::runAll();


orkerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$worker = new Worker();
$worker->onWorkerStart = function()
{
    // Websocket protocol for client.
    $ws_connection = new AsyncTcpConnection("ws://echo.websocket.org:80");
    $ws_connection->onConnect = function($connection){
        $connection->send('hello');
    };
    $ws_connection->onMessage = function($connection, $data){
        echo "recv: $data\n";
    };
    $ws_connection->onError = function($connection, $code, $msg){
        echo "error: $msg\n";
    };
    $ws_connection->onClose = function($connection){
        echo "connection closed\n";
    };
    $ws_connection->connect();
};
Worker::runAll();


/**
 * run with command
 * php start.php start
 */

use \Workerman\Worker;
use \Workerman\Clients\Tcp;
80';
    $tcp = new Tcp($url);
    $tcp->onConnect = function ($client) {
        $client->send('123');
    };
    $tcp->onReceive = function ($client,$data) {
        var_dump($data);
    };
    $tcp->connect();
};
$worker->count = 1;
Worker::$stdoutFile = '/tmp/oauth.log';
Worker::$logFile = __DIR__ . '/workerman.log';
Worker::$pidFile = __DIR__ . "/" . str_replace('/', '_', __FILE__) . ".pid";
// 运行所有服务
Worker::runAll();


/**
 * run with command
 * php start.php start
 */

use \Workerman\Worker;
use \Workerman\Clients\Ws;
use \Swoole\Http\Client;
w Ws($url);
    $tcp->onConnect = function (Client $client) {
        var_dump($client);
    };
    $tcp->onMessage = function (Client $client,$data) {
        $client->push('{"type":"ping"}');
        var_dump($data);
    };
    $tcp->connect();
};
$worker->count = 1;
Worker::$stdoutFile = '/tmp/oauth.log';
Worker::$logFile = __DIR__ . '/workerman.log';
Worker::$pidFile = __DIR__ . "/" . str_replace('/', '_', __FILE__) . ".pid";
// 运行所有服务
Worker::runAll();


orkerman\Worker;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function() {
   swoole_async_dns_lookup("www.baidu.com", function($host, $ip){
       echo "{$host} : {$ip}\n";
   });
};
$worker->onMessage = function($connection, $host) {

};

Worker::runAll();


/**
 * run with command
 * php start.php start
 */

use \Workerman\Worker;
use \Workerman\Clients\Http;

.net';
    $request_method = 'get';
    $data = ['uid'=>1];
    $http = new Http($url, $request_method);
    $http->onResponse = function ($cli) {
        var_dump($cli->body);
    };
    $http->request($data);
};
$worker->count = 1;
Worker::$stdoutFile = '/tmp/oauth.log';
Worker::$logFile = __DIR__ . '/workerman.log';
Worker::$pidFile = __DIR__ . "/" . str_replace('/', '_', __FILE__) . ".pid";
// 运行所有服务
Worker::runAll();


orkerman\Worker;
use \Swoole\Mysql;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function () {
    global $mysql;
    $mysql = new Mysql;
    $server = array(
        'host' => '192.168.56.102',
        'port' => 3306,
        'user' => 'test',
        'password' => 'test',
        'database' => 'test',
        'charset' => 'utf8', //指定字符集
        'timeout' => 2,  // 可选:连接超时时间(非查询超时时间),默认为SW_MYSQL_CONNECT_TIMEOUT(1.0)
    );

    $mysql->connect($server, function (Mysql $db, $r) {
        if ($r === false) {
            var_dump($db->connect_errno, $db->connect_error);
            die;
        }
    });
};
$worker->onMessage = function ($connection, $data) {
    global $mysql;
    $sql = 'show tables';
    $mysql->query($sql, function (Mysql $db, $r) {
        if ($r === false) {
            var_dump($db->error, $db->errno);
        } elseif ($r === true) {
            var_dump($db->affected_rows, $db->insert_id);
        }
        var_dump($r);
    });
};
Worker::runAll();


orkerman\Worker;
use \Swoole\Redis;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function () {
    global $client;
    $client = new Redis;
    $client->connect('127.0.0.1', 6379, function (Redis $client, $result) {
        echo "connect\n";
        var_dump($result);
        $db = 0;
        $client->select($db);
        $password = '111111';
        $client->auth($password);
    });
};
$worker->onMessage = function ($connection, $data) {
    global $client;
    $client->set('key', 'swoole', function (Redis $client, $result) {
        var_dump($result);
        $client->get('key', function (Redis $client, $result) {
            var_dump($result);
            $client->close();
        });
    });
};
Worker::runAll();


orkerman\Worker;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function () {
$zmq = new Swoole\Async\ZMQ();
$zmq->connect('tcp://0.0.0.0:5555');
Swoole\Timer::tick(1000, function () use ($zmq)
{
    static $i = 0;
    $msg = "hello-" . $i++;
    echo "Sending: $msg\n";
    $zmq->send($msg);
});
};
$worker->onMessage = function ($connection, $data) {

};
Worker::runAll();
 test.php start  
 test.php start -d  
 test.php status  
 test.php stop  
 test.php restart  
 test.php reload