gpt4 book ai didi

php - 无需 cli 即可运行 Ratchet php 服务器

转载 作者:行者123 更新时间:2023-12-04 16:13:47 25 4
gpt4 key购买 nike

我正在与 Ratchet 实现 websocket 实时聊天
它工作正常,问题是我需要运行 server.php 通过命令行这样服务器就可以工作了,我不能直接运行文件
我尝试通过:

exec()



和其他方法但它无法运行服务器,有人有替代方法或解决方案吗?

服务器.php

<?php

use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;


require 'vendor/autoload.php';
require 'class/SimpleChat.php';


$server = IoServer::factory(
new HttpServer(
new WsServer(
new SimpleChat()
)
),
8080
);

$server->run();


/class/SimpleChat.php

<?php

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class SimpleChat implements MessageComponentInterface
{
/** @var SplObjectStorage */
protected $clients;

/**
* SimpleChat constructor.
*/
public function __construct()
{
conectados
$this->clients = new \SplObjectStorage;
}

/**
* @param ConnectionInterface $conn
*/
public function onOpen(ConnectionInterface $conn)
{

$this->clients->attach($conn);
echo "Cliente conectado ({$conn->resourceId})" . PHP_EOL;
}

/**
* @param ConnectionInterface $from
* @param string $data
*/
public function onMessage(ConnectionInterface $from, $data)
{

$data = json_decode($data);
$data->date = date('d/m/Y H:i:s');


foreach ($this->clients as $client) {
$client->send(json_encode($data));
}

echo "User {$from->resourceId} sent you a message" . PHP_EOL;
}

/**
* @param ConnectionInterface $conn
*/
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
echo "User {$conn->resourceId} Disconnected" . PHP_EOL;
}

/**
* @param ConnectionInterface $conn
* @param Exception $e
*/
public function onError(ConnectionInterface $conn, \Exception $e)
{

$conn->close();

echo "Error {$e->getMessage()}" . PHP_EOL;
}
}

最佳答案

我遇到了同样的问题。使用 cURL 解决了它。使用以下命令来
无需 cli 即可启动 Ratchet 服务器。

$handle = curl_init();
$url = 'Path-to-your-server-file/App/server.php';
curl_setopt($handle,CURLOPT_URL,$url);
curl_setopt($handle, CURLOPT_TIMEOUT, 1);
$data = curl_exec($handle);
curl_close($handle);
print_r($handle);

关于php - 无需 cli 即可运行 Ratchet php 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56999016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com