Ratchet Websocket can't receive data from client?

随声附和 提交于 2019-12-12 00:42:36

问题


I have a real time web application with the frameWork Symfony. I need to send data from client to the webscket server.so I have try this :

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

};

It does't show any error and in the server side I have this : The Server Code :

$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();

and this is my App code :

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) {
    }
}

The problem I can't find were I ll catch the message sent from client?


回答1:


Your message does not conform to the WAMP standard. Take a look at AutobahnJS for your client connection. Also take a look at the Ratchet Push Integration Tutorial, which has a functional example.




回答2:


Remember that the WAMP standard is only a suggestion for Push Integration(ZMQ).You can use to handle this mechanism on client side by pure javascript, without using any library such as AutobahnJS

Server code:

    // 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();

Your Application code doesn't have need to Topic class (WAMP Class) for message pattern for this reason it doesn't need to use WAMP and AutobahnJS.

App code for example:

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) {}

}



回答3:


Watch the below slide.

http://wamp-proto.org/

The client can communictate with the server via Remote Procedure Calls (RPC).

If you use AutobahnJS (http://autobahn.ws/js/), you can add this in your client.

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

In your Pusher class, you have use the below function catch the message sent from the client.

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


来源:https://stackoverflow.com/questions/17893586/ratchet-websocket-cant-receive-data-from-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!