我有一个PHP代码,它调用另一个PHP脚本来获取缓存的新数据" JSON字符串"。这是我当前的代码通信是什么
if( ! isset($_COOKIE['ICWS_SESSION']) ){
throw new exception('Not Clocked In');
}
if( ! isset($_COOKIE['ICWS_STATION']) ){
throw new exception('Missing Station Name');
}
$url = \classes\Cipher::decrypt($_COOKIE['ICWS_SESSION']);
$attrebutes = array('test');
//configure the connection
$conf = new ICWS\Config\Config($url, $_COOKIE['ICWS_STATION']);
//create a new instance of icws
$icws = new ICWS\Connection($conf, $attrebutes);
if ( !$icws->isLogged() ){
throw new exception('Something Went Wrong when trying to login');
}
$messaging = new ICWS\Messaging($icws);
while(1){
$messaging->processMessages();
$myQueue = array();
$myQueue = array_merge( (array) $messaging->getCallsQueue(), (array) $messaging->getCurrentUserStatusQueue()) ;
echo json_encode($myQueue);
sleep(1);
}
现在,我想在Ratchet PHP WebSocket中添加此代码,以确保每个用户与服务器建立1个TCP连接以处理此请求。
这是我当前的websocket实现。
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
//open connection
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
//handle incoming messages from the client
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from === $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
//handle close connection
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
//handle errors
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
以下是我启动此WebSocket的方法
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/../vendor/autoload.php';
require dirname(__DIR__) . '/icws/MyApp/Chat.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8010
);
$server->run();
问题
在我的实现中,我会在建立连接后添加循环吗?
这是我试过的
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class icws implements MessageComponentInterface {
protected $clients;
protected $messaging;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
//open connection
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
if( ! isset($_COOKIE['ICWS_SESSION']) ){
throw new exception('Not Clocked In');
}
if( ! isset($_COOKIE['ICWS_STATION']) ){
throw new exception('Missing Station Name');
}
$url = \classes\Cipher::decrypt($_COOKIE['ICWS_SESSION']);
$attrebutes = array('test');
//configure the connection
$conf = new ICWS\Config\Config($url, $_COOKIE['ICWS_STATION']);
//create a new instance of icws
$icws = new ICWS\Connection($conf, $attrebutes);
if ( !$icws->isLogged() ){
throw new exception('Something Went Wrong when trying to login');
}
$this->messaging = new ICWS\Messaging($icws);
}
//handle incoming messages from the client
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from === $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
//handle close connection
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
//handle errors
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}