使用 autobahn.js 的浏览器客户端无法连接到 Ratchet Websocket 服务器 [英] Browser client using autobahn.js can not connect to Ratchet Websocket Server

查看:31
本文介绍了使用 autobahn.js 的浏览器客户端无法连接到 Ratchet Websocket 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题:我使用 PHP Ratchet 开发了一个 Ratchet Web Socket Serversocketo.me

I'm struggling with a problem : I developed a Ratchet Web Socket Server using PHP Ratchet socketo.me

我的服务器已启动并正在侦听端口 8082 :

My server is up and listening to port 8082 :

require 'vendor/autoload.php';   

    $loop   = React\EventLoop\Factory::create();
    $pusher = new MyApp\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:5551'); // 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(8082); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
            )
        ),
        $webSock
    );

    $loop->run();

我的帖子脚本:post.php

My post script : post.php

    <?php
        error_reporting(E_ALL);
        ini_set("display_errors", 1);

        // post.php ???
        // This all was here before  ;)
        $entryData = array(
            'category' => 'kittensCategory'
          , 'title'    => 'title : check websocket'
          , 'article'  => 'article : check websocket'
          , 'when'     => time()
        );

        // This is our new stuff
        $context = new ZMQContext();
        $socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'Pusher');
        $socket->connect("tcp://127.0.0.1:5551");

        $socket->send(json_encode($entryData));

我的 pusher.php

My pusher.php

<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;

class Pusher implements WampServerInterface {
    /**
     * A lookup of all the topics clients have subscribed to
     */
    protected $subscribedTopics = array();

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

    public function onSubscribe(ConnectionInterface $conn, $topic) {
        print_r($topic);
        die;
        $this->subscribedTopics[$topic->getId()] = $topic;
    }

    /**
     * @param string JSON'ified string we'll receive from ZeroMQ
     */
    public function onBlogEntry($entry) {
        $entryData = json_decode($entry, true);

        // If the lookup topic object isn't set there is no one to publish to
        if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
            return;
        }

        $topic = $this->subscribedTopics[$entryData['category']];

        // re-send the data to all the clients subscribed to that category
        $topic->broadcast($entryData);
    }


    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
    }
    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 onClose(ConnectionInterface $conn) {
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->close();
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

我通过从 post.php 发送数据来检查我的服务器是否已启动,并且我在 onBlogEntry() 方法

I checked that my server is up by sending data from post.php and I'm getting data in onBlogEntry() method


问题是当浏览器尝试打开与我的服务器的连接时,它总是出现连接超时错误:

The Problem is when the browser tries open a connection to my server it always giving Connection timeout error :

<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
var conn = new ab.Session('ws://simplifi.org:8082',
    function() {
        conn.subscribe('kittensCategory', function(topic, data) {
            console.log('New article published to category "' + topic + '" : ' + data.title);
        });
    },
    function() {
        console.warn('WebSocket connection closed');
    },
    {'skipSubprotocolCheck': true}
);
</script>

任何人都知道出了什么问题,请帮忙.

Any one has any idea about what is wrong please help.

推荐答案

好的,端口被防火墙阻止了,我打开它,它工作正常.

Ok the port was blocked form Firewall I opened it and it working fine.

这篇关于使用 autobahn.js 的浏览器客户端无法连接到 Ratchet Websocket 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆