【发布时间】:2013-08-12 22:06:54
【问题描述】:
我正在尝试从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
Chat.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();
composer.json
{
"require": {
"cboden/Ratchet": "0.2.*"
},
"autoload": {
"psr-0": {
"MyChat": "src"
}
}
}
我的 php.exe 在 C:\wamp\bin\php\php5.4.12 中。非常感谢您的建议,我真的不知道我在哪里弄错了。
【问题讨论】:
-
您是否已将 MyChatBundle 添加到 app/AppKernel.php 中?
-
@mattexx 我没有“app”目录,所以显然没有 AppKernel.php,教程中没有任何地方建议它,项目的存储库也没有github 有任何“app”目录。我已经发布了我的项目的完整树(除了带有依赖项的供应商目录)
-
你能发布你用来运行服务器脚本的确切命令吗?你是从 www 还是 bin 运行?
-
检查你的本地网络路由防火墙,它可以是像8080这样的带端口。或者你的客户端机器与服务器机器不同,检查两边。
-
@mattexx php bin/chat-server.php 是我正在使用的命令,如文档中所述。
标签: php websocket chat phpwebsocket ratchet