Cai*_*ins 11 php webserver websocket composer-php ratchet
我正在尝试使用Ratchet库来使用位于http://socketo.me/的 WebSockets,但是在Ubuntu中从命令行运行服务器脚本时遇到了一些问题.
在成功安装了composer和Ratchet之后,我正在http://socketo.me/docs/hello-world上按照基本聊天应用程序的教程进行操作,我正在运行它.我的文件结构,其中websockets是我的项目文件夹,是:
kingsconflict
websockets
chat.php
chat-server.php
composer.json
vendor
autoload.php
(dependecies included by composer for Ratchet)
Run Code Online (Sandbox Code Playgroud)
输入"sudo php chat-server.php"时出现的错误是"PHP致命错误:require():无法打开所需的'/var/www/kingsconflict/vendor/autoload.php'(include_path ='.:/ usr/share/php:/ usr/share/pear')在/var/www/kingsconflict/websockets/chat-server.php第5行".好像它正在尝试打开/var/www/kingsconflict/vendor/autoload.php,但实际的路径是/var/www/kingsconflict/websockets/vendor/autoload.php,我不知道为什么会这样做.
聊天server.php
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php'; // Error here
$server = IoServer::factory(
new Chat()
, 8080
);
$server->run();
Run Code Online (Sandbox Code Playgroud)
我尝试使用下面的行修改错误的行,并且我停止收到错误但是我收到一个新错误"PHP致命错误:类'MyApp\Chat'未找到"这导致我相信此修复不正确.
require ('./vendor/autoload.php');
Run Code Online (Sandbox Code Playgroud)
其他文件的代码与Ratchet教程中显示的相同,但万一我将在下面发布
chat.php
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
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);
}
}
}
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";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Run Code Online (Sandbox Code Playgroud)
composer.json
{
"autoload": {
"psr-0": {
"MyApp": "src"
}
},
"require": {
"cboden/Ratchet": "0.2.*"
}
}
Run Code Online (Sandbox Code Playgroud)
autoload.php(没有编辑这个但是到底是什么)
<?php
// autoload.php generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit0964ef3a5e66723368300f04c3206ca1::getLoader();
Run Code Online (Sandbox Code Playgroud)
mez*_*ndo 16
假设你的composer.json是
{
"autoload": {
"psr-0": {
"MyApp": "src"
}
},
"require": {
"cboden/Ratchet": "0.3.*"
}
}
Run Code Online (Sandbox Code Playgroud)
在启动bin/chat-server.php之前,您必须使用以下命令更新自动加载文件:
$ composer.phar update
Run Code Online (Sandbox Code Playgroud)