问题描述
这是一个服务器脚本,用于初始化 Web 套接字服务器并侦听与 8001 端口的客户端连接。我的网站是关于投标的。因此,当有人点击出价按钮时,我想这样做,该特定用户详细信息(用户名和头像)将显示给所有其他出价者。所以这就是我使用带有 PHP Laravel 的网络套接字的原因,它在 localhost 上工作正常,但在实时服务器上出现错误。下面附上错误图片。
composer.json
{
"autoload": {
"psr-4": {
"MyApp\\": "src"
}
},"require": {
"cboden/ratchet": "^0.4"
}
}
消息组件接口
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();
}
}
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.PHP';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),8080
);
$server->run();
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)