2

我正在尝试 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!!!在浏览器控制台中打印出来?

4

1 回答 1

3

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

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

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

谜团已揭开!

于 2015-03-03T01:12:30.390 回答