1
我正在嘗試高速公路,並且無法使演示代碼正常工作。客戶端未使用演示代碼接收已發佈的事件?
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!!!
已經在瀏覽器控制檯被打印出來?
這是正確的,默認情況下,發佈客戶端不接收消息。如果您需要更改此行爲,則可以使用'exclude_me = false'選項。 https://github.com/tavendo/WAMP/blob/master/spec/advanced.md#publisher-exclusion – daviddan 2015-03-03 12:29:31