如何处理 PHP 中的套接字
How to handle sockets in PHP
我正在使用 Ratchett 通过 PHP 在我的网络服务器上创建套接字。
目前我有 A - 服务器,B - 寻找数据库更改的设备(在 A) 和 C - 对数据库进行更改的设备(在 A 中)带有 curl
请求。
当前,当 C 进行更改时,我将更改存储在 A 上的数据库中。意思是 B 每秒通过套接字检查 A 是否有项目通过向 [=34= 发送请求在数据库中]A 其中 A returns 一个响应。这是 B 资源的腰部。当 C 对套接字进行更改以更新 B.
时,我需要一种方法
这是套接字的代码:
<?php
namespace Notify;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require "/NAS/notify/db.php";
class Note implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
$stack = array();
foreach ($this->clients as $client) {
if ($from === $client) { //current client only
$query = getNotifications($msg);
if($query){
while($row = mysqli_fetch_assoc($query)){
array_push($stack, $row);
deleteNotification($row['id'], $msg);
}
$client->send(json_encode($stack));
}
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
其中 getNotifications()
获取数据库中的更改,deleteNotification()
删除它。
想法:
- 我考虑过将
$client
对象存储在数据库中,这样当 C 进行更改时它可以使用 $client->send()
但我不认为那可能吗?
- 我可以有一个
while(true)
循环,其中包含 $query = getNotifications($msg);
代码 - 对于 A. 来说太糟糕了
您还有其他想法吗?或者实现上面的方法。
考虑使用发布-订阅模式,其中寻找对象更新的客户端 "X" 订阅一个主题,然后让服务器发布该主题。 Ratchet 可以使用 WAMPv1 处理此问题:http://socketo.me/docs/wamp or you could use Thruway which is WAMPv2 建立在 ratchet 之上。
我正在使用 Ratchett 通过 PHP 在我的网络服务器上创建套接字。
目前我有 A - 服务器,B - 寻找数据库更改的设备(在 A) 和 C - 对数据库进行更改的设备(在 A 中)带有 curl
请求。
当前,当 C 进行更改时,我将更改存储在 A 上的数据库中。意思是 B 每秒通过套接字检查 A 是否有项目通过向 [=34= 发送请求在数据库中]A 其中 A returns 一个响应。这是 B 资源的腰部。当 C 对套接字进行更改以更新 B.
时,我需要一种方法这是套接字的代码:
<?php
namespace Notify;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require "/NAS/notify/db.php";
class Note implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
$stack = array();
foreach ($this->clients as $client) {
if ($from === $client) { //current client only
$query = getNotifications($msg);
if($query){
while($row = mysqli_fetch_assoc($query)){
array_push($stack, $row);
deleteNotification($row['id'], $msg);
}
$client->send(json_encode($stack));
}
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
其中 getNotifications()
获取数据库中的更改,deleteNotification()
删除它。
想法:
- 我考虑过将
$client
对象存储在数据库中,这样当 C 进行更改时它可以使用$client->send()
但我不认为那可能吗? - 我可以有一个
while(true)
循环,其中包含$query = getNotifications($msg);
代码 - 对于 A. 来说太糟糕了
您还有其他想法吗?或者实现上面的方法。
考虑使用发布-订阅模式,其中寻找对象更新的客户端 "X" 订阅一个主题,然后让服务器发布该主题。 Ratchet 可以使用 WAMPv1 处理此问题:http://socketo.me/docs/wamp or you could use Thruway which is WAMPv2 建立在 ratchet 之上。