如何在php CLI客户端共享数据?我创建了swoole WebSocket Server,它在CLI模式下运行。我想保存套接字信息。
但每次保存的全局变量只有最后一个客户端信息。有没有办法在CLI模式下共享$ _SESSION等数据?我尝试了$ _SERVER和$ GLOBALS,但我无法做到。
$ _GLOBALS不能用于swoole,可以使用Swoole \ Table共享数据。在创建服务器之前,请初始化表以存储要共享的数据。
<?php
class Server {
public $table;
public __construct()
{
//initialize server code
...
//initialize table
$this->table = new Swoole\Table(1024);
$this->table->column('name', Table::TYPE_STRING, 4);
//other column
$this->table->create();
$this->table->set('user', ['name' => 'Bob']);
//get user
$this->table->get('user');
}
}