我正在尝试从http://socketo.me/docs/hello-world实现基本的聊天应用程序,但是我不断收到此错误。我试图移动文件,但没有成功,但我很确定我没有将文件放在正确的位置。我对作曲家、websockets 和 psr-0 完全陌生,我还有很多关于 PHP 的知识要学习。这是我的路径树和来源:
C:\wamp\www\
bin
chat-server.php
src
MyChat
Chat.php
vendor
{dependencies}+autoload.php
composer.json
composer.phar
composer.lock
聊天.php
<?php
namespace MyChat;
require dirname(__DIR__) . '\vendor\autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
protected $clients;
function __construct()
{
$this->clients=new \SplObjectStorage();
}
function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
function onClose(ConnectionInterface $conn)
{
echo "Connection closed: {$conn->resourceId} \n";
$this->clients->detach($conn);
}
function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occured: {$e->getMessage()}. Closing connection... \n";
$conn->close();
}
function onMessage(ConnectionInterface $from, $msg)
{
$receivers=count($this->clients)-1;
foreach($this->clients as $client)
{
if($client!=$from)
{
$client->send($msg);
}
}
}
}
聊天服务器.php
<?php
require dirname(__DIR__) . '\vendor\autoload.php';
use Ratchet\Server\IoServer;
use MyChat\Chat;
$server= IoServer::factory (new Chat() ,8080,'0.0.0.0');//0.0.0.0 is default, means accept all connections
$server->run();
作曲家.json
{
"require": {
"cboden/Ratchet": "0.2.*"
},
"autoload": {
"psr-0": {
"MyChat": "src"
}
}
}
我的 php.exe 在 C:\wamp\bin\php\php5.4.12 中。我真的很感谢你的建议,我真的不知道我在哪里弄错了。