客户端未使用演示代码接收已发布的事件?
Client not receiving published events using demo code?
我正在试用 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!!!
是否应该在浏览器控制台中打印出来?
我在 github 回购中的一张票中找到 post 说
the publishing client will not receive the event message by default.
使用 JS 打开另一个选项卡导致消息被发送到之前打开的选项卡。
谜底已解!
我正在试用 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!!!
是否应该在浏览器控制台中打印出来?
我在 github 回购中的一张票中找到 post 说
the publishing client will not receive the event message by default.
使用 JS 打开另一个选项卡导致消息被发送到之前打开的选项卡。
谜底已解!