客户端未使用演示代码接收发布的事件?

时间:2015-03-02 18:55:49

标签: thruway

我正在试用Thruway并且无法让演示代码生效。

Javascript代码:

<script src="https://autobahn.s3.amazonaws.com/autobahnjs/latest/autobahn.js"></script>
<script>
//    var autobahn = require('autobahn');

    var connection = new autobahn.Connection({url: 'ws://dev.mysite.com:9090/', realm: 'realm1'});

    connection.onopen = function (session) {

        // 1) subscribe to a topic
        function onevent(args) {
            console.log("Event:", args[0]);
        }
        session.subscribe('com.myapp.hello', onevent);

        // 2) publish an event
        session.publish('com.myapp.hello', ['Hello, world!']);
    };

    connection.open();
</script>

我有SimpleWsServer.php正在运行:

<?php

require 'bootstrap.php';

use Thruway\Peer\Router;
use Thruway\Transport\RatchetTransportProvider;

$router = new Router();

$transportProvider = new RatchetTransportProvider("127.0.0.1", 9090);

$router->addTransportProvider($transportProvider);

$router->start();

我有SimpleClient.php正在运行(我删除了RPC代码,因为我只想将消息从服​​务器推送到客户端):

<?php

require 'bootstrap.php';

use Thruway\ClientSession;
use Thruway\Connection;

$onClose = function ($msg) {
    echo $msg;
};

$connection = new Connection(
    [
        "realm"   => 'realm1',
        "onClose" => $onClose,
        "url"     => 'ws://127.0.0.1:9090',
    ]
);

$connection->on(
    'open',
    function (ClientSession $session) {

        // 1) subscribe to a topic
        $onevent = function ($args) {
            echo "Event {$args[0]}\n";
        };
        $session->subscribe('com.myapp.hello', $onevent);

        // 2) publish an event
        $session->publish('com.myapp.hello', ['Hello, world from PHP!!!'], [], ["acknowledge" => true])->then(
            function () {
                echo "Publish Acknowledged!\n";
            },
            function ($error) {
                // publish failed
                echo "Publish Error {$error}\n";
            }
        );

//        // 3) register a procedure for remoting
//        $add2 = function ($args) {
//            return $args[0] + $args[1];
//        };
//        $session->register('com.myapp.add2', $add2);
//
//        // 4) call a remote procedure
//        $session->call('com.myapp.add2', [2, 3])->then(
//            function ($res) {
//                echo "Result: {$res}\n";
//            },
//            function ($error) {
//                echo "Call Error: {$error}\n";
//            }
//        );
    }

);

$connection->open();

在我看来,演示代码在订阅后会向客户端发送消息Hello, world from PHP!!!,但我在浏览器的控制台中没有看到消息。

我知道客户端正在连接到服务器,因为SimpleClient.php将以下内容输出到终端:

2015-03-02T19:47:24.5464800 debug      [Thruway\Transport\PawlTransportProvider 13800] Received: [36,1574620859,33562629,{},["Hello, world!"]]
2015-03-02T19:47:24.5470880 debug      [Thruway\Peer\Client 13800] Client onMessage: [Thruway\Message\EventMessage]
Event Hello, world!

我是否遗漏了某些内容,或者应该在浏览器控制台中打印出Hello, world from PHP!!!

1 个答案:

答案 0 :(得分:3)

我在github repo中的一张票中发现了一个帖子

  

默认情况下,发布客户端不会收到事件消息。

使用JS打开另一个选项卡导致消息被发送到先前打开的选项卡。

神秘解决了!