【问题标题】:Ratchet Websocket can't receive data from client?Ratchet Websocket 无法从客户端接收数据?
【发布时间】:2013-07-27 02:10:22
【问题描述】:

我有一个带有框架 Symfony 的实时 Web 应用程序。我需要将数据从客户端发送到 webscket 服务器。所以我尝试了这个:

var conn = new WebSocket('ws://127.0.0.1:8080');
console.log (conn);
conn.onopen = function (e) { 
                            console.log ("Connection established!");
                conn.send("xoxo");

};

它没有显示任何错误,在服务器端我有这个: 服务器代码:

$app=new AggregateApplication();
$loop   = \React\EventLoop\Factory::create();
$context = new \React\ZMQ\Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($app, 'EditMessage'));
$webSock = new \React\Socket\Server($loop);
$webSock->listen(8080, '127.0.0.1');
$handler = $this->getContainer()->get('session.handler');
$server=new \Ratchet\Wamp\WampServer($app);
$server = new SessionProvider($server, $handler);
$webServer = new \Ratchet\Server\IoServer(new     \Ratchet\WebSocket\WsServer($server),$webSock);
$loop->run();

这是我的应用程序代码:

class AggregateApplication implements WampServerInterface {
    protected $clients;
    protected $comming;
    public function __construct() {
        $this->clients = array();
        $this->comming = array();
    }

    public function onOpen(ConnectionInterface $conn){
        $this->clients[array_shift($this->comming)]=$conn;
        echo "New connection! ".array_shift($this->comming)." ({$conn->resourceId})\n";
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params){

    }
     public function onSubscribe(ConnectionInterface $conn, $topic){
        echo "onSubscribe";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $topic){

    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible){

    }
    public function onClose(ConnectionInterface $conn) {
        unset($this->clients[array_search($conn, $this->clients)]);
       echo 'close connection ';
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

我找不到的问题是我会捕获客户端发送的消息吗?

【问题讨论】:

    标签: php symfony websocket ratchet


    【解决方案1】:

    您的消息不符合WAMP standard。查看AutobahnJS 以了解您的客户端连接。还可以看看Ratchet Push Integration Tutorial,它有一个功能示例。

    【讨论】:

      【解决方案2】:

      请记住,WAMP standard 只是推送集成的建议(ZMQ)。您可以使用 纯 javascript 在客户端处理此机制,而无需使用任何库,例如 @ 987654323@

      服务器代码:

          // Create loop for listen    
          $loop = React\EventLoop\Factory::create();
          $pusher = new Pusher;
      
          // Listen for the web server to make a ZeroMQ push after an ajax request
          $context = new React\ZMQ\Context($loop);
          $pull = $context->getSocket(ZMQ::SOCKET_PULL);
          $pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
          $pull->on('message', array($pusher, 'onBlogEntry'));
      
          // Set up our WebSocket server for clients wanting real-time updates
          $webSock = new React\Socket\Server($loop);
          $webSock->listen(8080, '127.0.0.1'); // Binding to 127.0.0.1 means remotes can connect
          $webServer = new Ratchet\Server\IoServer(
                  new Ratchet\WebSocket\WsServer(
                  $pusher
                  ), $webSock
          );
      
          // run server for listen websocket base connection
          $loop->run();
      

      您的应用程序代码不需要 Topic 类(WAMP 类)用于消息模式,因此它不需要使用 WAMP 和 AutobahnJS。

      应用代码示例:

      class Pusher implements MessageComponentInterface {
      
          public function __construct() {
              $this->clients = new \SplObjectStorage;
          }
      
          public function onOpen(ConnectionInterface $conn) {}
      
          public function onMessage(ConnectionInterface $from, $msg) {}
      
          public function onClose(ConnectionInterface $conn) {}
      
          public function onError(ConnectionInterface $conn, \Exception $e) {}
      
          public function onBlogEntry($entry) {}
      
      }
      

      【讨论】:

      • 想了很多你能建议我一个 javascript 库,我可以用它来连接 messageCompnentInterface。我知道我可以直接使用 websocket 类,但我需要一个更安全的 js 库..
      【解决方案3】:

      观看下面的幻灯片。

      http://wamp-proto.org/

      客户端可以通过远程过程调用 (RPC) 与服务器通信。

      如果你使用 AutobahnJS (http://autobahn.ws/js/),你可以在你的客户端中添加它。

      session.call('myaction', [mydata]).then(
          function (res) {
              console.log("Result:", res);
          }
      );
      

      在您的 Pusher 类中,您已使用以下函数捕获客户端发送的消息。

      public function onCall(ConnectionInterface $conn, $id, $topic, array $params)
      {
          // do what you want
      }
      

      【讨论】:

        猜你喜欢
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        相关资源
        最近更新 更多