是否可以在棘轮连接中存储额外的变量
Is it possible to store additional variables in a ratchet connection
这是我的onMessage
public function onMessage(ConnectionInterface $from, $msg) {
$tempMessage = json_decode($msg, TRUE);
if ($tempMessage['type'] == 'online') {
foreach ($this->clients as $client) {
if ($client == $from) {
echo "client " . $from->resourceId . "(" . $from->remoteAddress . ") is online\n";
}
}
}
}
是否可以保存此 $client 对象中的值供以后参考?
我知道可以为此保留一个数组,但它会变得复杂
如文档中所示,我将客户端存储在 SplObjectStorage
中
通用方式
如果您查看 \SplObjectStorage
的 php documentation,您会发现您可以向对象添加信息,因此根据您的代码,您可以像这样添加数据
$this->clients[$from]->setInfo(['myData' => 'foo']);
然后像这样取回
$data = $this->clients[$from]->getInfo();
var_dump($data); // $data = ['myData' => 'foo']
}
Easy/Fast 方式
免责声明:这只是在onOpen中设置数据(例如:cookies),之后每次都会克隆连接传递给 onMessage 使原始连接不可变,这就是为什么我不推荐此解决方案来设置与原始连接无关的数据,因为它可能导致难以调试的错误
Since the connection is a php class you are allowed to add properties
to it as long as the property is not already defined as protected or
private
In the source code of the client class (In
\Ratchet\AbstractConnectionDecorator
) you will find
public function __set($name, $value) {
$this->wrappedConn->$name = $value;
}
public function __get($name) {
return $this->wrappedConn->$name;
}
Meaning the class is just a wrapper for a \React\Socket\Connection
which has no setter/getter.
因此您可以像操作对象一样操作属性
$client->myData = $data;
var_dump($client->myData);
另一种方式
与其将客户存储在 \SplObjectStorage
中,不如将它们存储在键控数组中并使用 spl_object_hash
生成密钥
PS:这已经是 SplObjectStorage 引擎盖下发生的事情,所以这是重新发明轮子
/**
* @var array[]
*/
protected $clients = [];
public function onOpen( ConnectionInterface $conn ) {
$this->clients[spl_object_hash( $conn )] = [ 'connection' => $conn, 'myData' => "My data" ];
}
public function onMessage( ConnectionInterface $conn, $msg ) {
$myData = $this->clients[spl_object_hash( $conn )]['myData'];
}
这是我的onMessage
public function onMessage(ConnectionInterface $from, $msg) {
$tempMessage = json_decode($msg, TRUE);
if ($tempMessage['type'] == 'online') {
foreach ($this->clients as $client) {
if ($client == $from) {
echo "client " . $from->resourceId . "(" . $from->remoteAddress . ") is online\n";
}
}
}
}
是否可以保存此 $client 对象中的值供以后参考?
我知道可以为此保留一个数组,但它会变得复杂
如文档中所示,我将客户端存储在 SplObjectStorage
通用方式
如果您查看 \SplObjectStorage
的 php documentation,您会发现您可以向对象添加信息,因此根据您的代码,您可以像这样添加数据
$this->clients[$from]->setInfo(['myData' => 'foo']);
然后像这样取回
$data = $this->clients[$from]->getInfo();
var_dump($data); // $data = ['myData' => 'foo']
}
Easy/Fast 方式
免责声明:这只是在onOpen中设置数据(例如:cookies),之后每次都会克隆连接传递给 onMessage 使原始连接不可变,这就是为什么我不推荐此解决方案来设置与原始连接无关的数据,因为它可能导致难以调试的错误
Since the connection is a php class you are allowed to add properties to it as long as the property is not already defined as protected or private
In the source code of the client class (In
\Ratchet\AbstractConnectionDecorator
) you will findpublic function __set($name, $value) { $this->wrappedConn->$name = $value; } public function __get($name) { return $this->wrappedConn->$name; }
Meaning the class is just a wrapper for a
\React\Socket\Connection
which has no setter/getter.
因此您可以像操作对象一样操作属性
$client->myData = $data;
var_dump($client->myData);
另一种方式
与其将客户存储在 \SplObjectStorage
中,不如将它们存储在键控数组中并使用 spl_object_hash
生成密钥
PS:这已经是 SplObjectStorage 引擎盖下发生的事情,所以这是重新发明轮子
/**
* @var array[]
*/
protected $clients = [];
public function onOpen( ConnectionInterface $conn ) {
$this->clients[spl_object_hash( $conn )] = [ 'connection' => $conn, 'myData' => "My data" ];
}
public function onMessage( ConnectionInterface $conn, $msg ) {
$myData = $this->clients[spl_object_hash( $conn )]['myData'];
}