11、Workerman 基本使用 - 共享数据

作者: 温新

图书: 【Workerman 基本使用】

阅读: 405

时间: 2024-11-23 02:24:28

hi,我是温新,一名 PHPer

GlobalData 基本使用

1、安装组件

$ composer require workerman/globaldata --ignore-platform-req=ext-pthreads

2、使用共享数据

<?php
/**
* global.php
*/
    
use Workerman\Worker;
use Workerman\Connection\TcpConnection;

require_once __DIR__ . '/vendor/autoload.php';

$globalServer = new GlobalData\Server('0.0.0.0', 2207);

$worker = new Worker('tcp://0.0.0.0:8888');

// 初始化全局变量 $global
// 用于存储指向 GlobalData 客户端实例的引用
$global = null;

// 在 Worker 启动时创建并初始化 GlobalData 客户端
$worker->onWorkerStart = function () use (&$global) {
    // 连接到运行在 127.0.0.1:2207 的 GlobalData 服务端
    $global = new GlobalData\Client('127.0.0.1:2207');
};

$worker->onMessage = function (TcpConnection $connection, $data) use (&$global) {
    // 输出当前全局变量 username 的值(赋值前)
    echo '赋值之前:' . $global->username . PHP_EOL;
    // 将全局变量username设置为新值“丽丽”
    $global->username = '丽丽' . PHP_EOL;
    // 输出设置后的全局变量 username 的值(赋值后)
    echo '赋值之后:' . $global->username;

    $connection->send(var_export($global->username));
};

Worker::runAll();

3、测试

# 服务端
$ php global.php start
Workerman[global.php] start in DEBUG mode
------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.1.15          PHP version:8.2.0           Event-Loop:\Workerman\Events\Event
-------------------------------------------- WORKERS ---------------------------------------------
proto   user            worker              listen                  processes    status           
tcp     codeing         globalDataServer    frame://0.0.0.0:2207    1             [OK]            
tcp     codeing         none                tcp://0.0.0.0:8888      1             [OK]            
--------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
赋值之前:
赋值之后:丽丽
'丽丽

# 客户端
$ telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hehe

GlobalData 注意事项:

  • 无法共享资源类型的数据,如 mysql 连接、socket 连接等;
  • 在 Workerman 环境中使用 Client,建议在 onXXX 回调中使用

原子添加

使用 add 方法可以进行原子添加。成功返回 true,失败返回 false。

<?php
/**
* global.php
*/
    
use Workerman\Worker;
use Workerman\Connection\TcpConnection;

require_once __DIR__ . '/vendor/autoload.php';

$globalServer = new GlobalData\Server('0.0.0.0', 2207);

$worker = new Worker('tcp://0.0.0.0:8888');

$global = null;

$worker->onWorkerStart = function () use (&$global) {
    $global = new GlobalData\Client('127.0.0.1:2207');
};

$worker->onMessage = function (TcpConnection $connection, $data) use (&$global) {
    // 输出当前全局变量 username 的值(赋值前)
    echo '赋值之前:' . $global->username . PHP_EOL;
    // 将全局变量username设置为新值“丽丽”
    if ($global->add('username', '丽丽')) {
        echo 'add success:' . $global->username;
    } else {
        echo 'fail add:' . $global->username;
    }


    $connection->send(var_export($global->username));
};

Worker::runAll();

服务端启动,开启 2 个客户端进行测试。

$ php global.php start
Workerman[global.php] start in DEBUG mode
------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.1.15          PHP version:8.2.0           Event-Loop:\Workerman\Events\Event
-------------------------------------------- WORKERS ---------------------------------------------
proto   user            worker              listen                  processes    status           
tcp     codeing         globalDataServer    frame://0.0.0.0:2207    1             [OK]            
tcp     codeing         none                tcp://0.0.0.0:8888      1             [OK]            
--------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
赋值之前:
add success:丽丽'丽丽'赋值之前:丽丽
fail add:丽丽'丽丽'

客户端

# 客户端 1
$ telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
1

# 客户端 2
$ telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
1
请登录后再评论