websocket - Ratchet Websockets - 灌输问题

标签 websocket installation ratchet

我一直在阅读很多书,但仍在努力让这个 Ratchet 工作。我正在努力让服务器端 shell 脚本启动并运行。供应商目录已从其原始安装位置移动,但已整体移动。当我运行 php composer.phar 更新或安装时,每次都说没有更新或安装。

这是我的目录结构:

/var/www/bin/socket.php (server script)
        /src/CommApp/Comm.php
        /htmp/ (public site)
        /vendor/
        composer.json
        composer.phar

composer.json
{
"autoload": {
    "psr-0": {
        "CommApp": "src"
    }
},
"require": {
    "cboden/ratchet": "0.3.*"
}

}

当我尝试使用服务器代码的顶级版本时,它可以工作,但我不知道如何绑定(bind)到不同的 IP,而不是 127.0.0.1,我需要绑定(bind)到 0.0.0.0。所以我在底部找到了这个较新版本的代码,它允许我绑定(bind)到我需要的 ip。底部代码给了我一个错误
PHP fatal error :在第 28 行的/var/www/bin/socket.php 中找不到“Ratchet\CommApp”类

socket.php
<?php
/**
require __DIR__ . '/../vendor/autoload.php';

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

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

$server->run();
 */

 // Ratchet/0.3
$app = new Ratchet\App('www.mysite.com',8080,'0.0.0.0');
$app->route('/CommApp',new Comm);
$app->run();

这是的代码com.php
<?php

namespace CommApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Comm implements MessageComponentInterface
{

    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        //store the new connection
        $this->clients->attach($conn);

        echo "someone connected\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        //send the message to all the other clients except the one who sent.
        foreach ($this->clients as $client) {
            if ($from !== $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
        echo "someone has disconnected";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

最佳答案

我不知道我是如何修复它的,但我重新安装了所有东西并且它起作用了。我认为这与 Ratchet 安装的初始位置有关,但我不能确定。

关于websocket - Ratchet Websockets - 灌输问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279401/

相关文章:

node.js - NodeJS WebSockets (ws) 模块是否实现了背压?

java - Eclipse - Java 已启动但返回退出代码 = 13

PHP 响应并行请求

php - 一些激进的代理会阻止不在端口 80 或 443 上的流量

java - 是否可以在 Spring 启动时将Web套接字与rest api一起使用?

ejabberd 的 WebSocket 支持

Android:Autobahn - 在 websocket 上取消请求/发送消息

python - 安装时运行脚本(vb.net 和 Python)

mysql - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : YES): Trying to install MySQL

PHP 安全 Websocket 客户端问题,需要非阻塞