作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将 websocket 用于投票应用程序。有两个部分:一个是聊天 channel ,另一个是命令 channel ,其中有关问题、可用答案、倒计时等的信息将传播。
在客户端,我使用 Autobahn|JS作为图书馆。我可以轻松订阅我的两个 channel (我可以在服务器日志上看到)
在服务器端,我使用 Thruway因为它符合 WAMP v2。
我的问题:我不知道如何在服务器端 Hook 消息以在每个主题上广播它们。看起来很简单,但我可以找到任何解决方案。
我猜是通过使用 InternalClient,但我不太确定……
一些代码:
JS客户端
var ws;
var connection = new autobahn.Connection({
url: 'ws://dev.test.com:8080/',
realm: 'com.test.dev'
});
connection.onopen = function (session) {
ws = session;
function onevent(args) {
console.log("Event:", args[0]);
}
session.subscribe('chat.test', onevent);
session.subscribe('cmd.test', onevent);
};
connection.open();
$('#message').on('keydown', function(event) {
var keycode = event.keyCode || event.which;
if(keycode == '13') {
event.preventDefault();
ws.publish('chat.test', [{token: token, message: $('#message').val()}]);
$('#message').val('');
}
});
<?php
require 'vendor/autoload.php';
use Thruway\Peer\Router;
use Thruway\Transport\RatchetTransportProvider;
class TestInternalClient extends Thruway\Peer\Client {
public function __construct() {
parent::__construct('com.test.dev');
}
public function onSessionStart($session, $transport) {
echo "--------------- Hello from InternalClient ------------\n";
$session->subscribe('chat.test', [$this, 'chat']);
}
public function chat($args, $kwArgs, $options) {
// Decode chat message here? How to broadcast them?
}
}
$router = new Router();
$router->addTransportProvider(new RatchetTransportProvider("0.0.0.0", 8080));
$router->addInternalClient(new TestInternalClient());
$router->start();
test@test:~/dev/wp-content/websockets$ php server.php
2017-01-03T17:48:19.4812750 notice Changing PHP precision from 14 to 16
2017-01-03T17:48:19.4831340 debug [Thruway\Peer\Router 21425] New router created
2017-01-03T17:48:19.4838930 info [TestInternalClient 21425] New client created
2017-01-03T17:48:19.4842930 info [Thruway\Peer\Router 21425] Starting router
2017-01-03T17:48:19.4874870 info [Thruway\Transport\RatchetTransportProvider 21425] Websocket listening on 0.0.0.0:8080
2017-01-03T17:48:19.4911550 info [Thruway\RealmManager 21425] Got prehello...
2017-01-03T17:48:19.4912050 debug [Thruway\RealmManager 21425] Creating new realm "com.test.dev"
2017-01-03T17:48:19.4928900 debug [Thruway\RealmManager 21425] Adding realm "com.test.dev"
2017-01-03T17:48:19.4943690 debug [TestInternalClient 21425] Client onMessage: [Thruway\Message\WelcomeMessage]
2017-01-03T17:48:19.4944080 info [TestInternalClient 21425] We have been welcomed...
--------------- Hello from InternalClient ------------
2017-01-03T17:48:19.4955960 debug [Thruway\Subscription\SubscriptionGroup 21425] Added subscription to "exact":"chat.test"
2017-01-03T17:48:19.4958000 debug [TestInternalClient 21425] Client onMessage: [Thruway\Message\SubscribedMessage]
2017-01-03T17:48:19.4965720 info [Thruway\Peer\Router 21425] Starting loop
2017-01-03T17:48:25.8358310 debug [Thruway\Transport\RatchetTransportProvider 21425] RatchetTransportProvider::onOpen
2017-01-03T17:48:25.9890380 debug [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([1,"com.test.dev",{"roles":{"caller":{"features":{"caller_identification":true,"progressive_call_results":true}},"callee":{"features":{"caller_identification":true,"pattern_based_registration":true,"shared_registration":true,"progressive_call_results":true,"registration_revocation":true}},"publisher":{"features":{"publisher_identification":true,"subscriber_blackwhite_listing":true,"publisher_exclusion":true}},"subscriber":{"features":{"publisher_identification":true,"pattern_based_subscription":true,"subscription_revocation":true}}}}])
2017-01-03T17:48:25.9895580 info [Thruway\RealmManager 21425] Got prehello...
2017-01-03T17:48:26.0515670 debug [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([32,2358366231642639,{},"chat.test"])
2017-01-03T17:48:26.0517410 debug [Thruway\Subscription\SubscriptionGroup 21425] Added subscription to "exact":"chat.test"
2017-01-03T17:48:26.0519070 debug [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([32,8132914407728460,{},"cmd.test"])
2017-01-03T17:48:26.0520030 debug [Thruway\Subscription\SubscriptionGroup 21425] Added subscription to "exact":"cmd.test"
2017-01-03T17:48:34.0115900 debug [Thruway\Transport\RatchetTransportProvider 21425] onMessage: ([16,3263467437225103,{},"chat.test",[{"token":"6b28af4056d650480e726e33986a2e790e34abb1","message":"Write from my chatbox"}]])
2017-01-03T17:48:34.0121500 debug [TestInternalClient 21425] Client onMessage: [Thruway\Message\EventMessage]
最佳答案
class TestInternalClient extends Thruway\Peer\Client {
public function __construct() {
parent::__construct('com.test.dev');
}
public function onSessionStart($session, $transport) {
echo "--------------- Hello from InternalClient ------------\n";
$session->subscribe('chat.test', function ($args, $kwArgs, $options) use ($session) {
// Get message contents
$token = $args[0]->token;
$message = $args[0]->message;
// publish to other people
$session->publish('some.topic', [[ 'message' => $message ]]);
});
//////////////////////////////////////////
// Subscribe to everything in the system
$session->subscribe('', function ($args, $argsKw, $details, $publicationId) {
$value = isset($args[0]) ? $args[0] : '';
echo 'Received ' . json_encode($value) . ' on topic ' . $details->topic . PHP_EOL;
}, [ 'match' => 'prefix' ]);
}
}
关于php - 如何使用 Thruway 在服务器端解析消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41448476/
聊天应用示例。 Mr A (07-Aug-2017 15:01) : hello all Mr B (07-Aug-2017 15:20) : hello Mr A 服务器日期时间 (dd-mmm-y
我想将 websocket 用于投票应用程序。有两个部分:一个是聊天 channel ,另一个是命令 channel ,其中有关问题、可用答案、倒计时等的信息将传播。 在客户端,我使用 Autobah
我正在尝试构建一个通知消息系统。我正在使用 SimpleWsServer.php服务器示例。当服务器上的任务完成时,我想向用户的浏览器推送通知。这需要使用 PHP 完成,我找不到显示此内容的教程。所有
我是一名优秀的程序员,十分优秀!