如何使用Throway在服务器端解析消息



我想将websocket用于投票应用程序。有两个部分:一个是聊天频道,另一个是命令通道,有关问题、可用答案、倒计时等的信息将在其中传播。

在客户端,我使用Autobahn|JS作为库。我可以很容易地订阅我的两个频道(我可以在服务器日志上看到)

在服务器端,我使用Throway,因为它符合WAMP v2。

我的问题:我不知道如何在服务器端挂接消息,以便在每个主题上广播它们。看起来很简单,但我能找到任何解决方案。

我想这是通过使用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服务器

<?php
require 'vendor/autoload.php';
use ThruwayPeerRouter;
use ThruwayTransportRatchetTransportProvider;
class TestInternalClient extends ThruwayPeerClient {
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      [ThruwayPeerRouter 21425] New router created
2017-01-03T17:48:19.4838930 info       [TestInternalClient 21425] New client created
2017-01-03T17:48:19.4842930 info       [ThruwayPeerRouter 21425] Starting router
2017-01-03T17:48:19.4874870 info       [ThruwayTransportRatchetTransportProvider 21425] Websocket listening on 0.0.0.0:8080
2017-01-03T17:48:19.4911550 info       [ThruwayRealmManager 21425] Got prehello...
2017-01-03T17:48:19.4912050 debug      [ThruwayRealmManager 21425] Creating new realm "com.test.dev"
2017-01-03T17:48:19.4928900 debug      [ThruwayRealmManager 21425] Adding realm "com.test.dev"
2017-01-03T17:48:19.4943690 debug      [TestInternalClient 21425] Client onMessage: [ThruwayMessageWelcomeMessage]
2017-01-03T17:48:19.4944080 info       [TestInternalClient 21425] We have been welcomed...
--------------- Hello from InternalClient ------------
2017-01-03T17:48:19.4955960 debug      [ThruwaySubscriptionSubscriptionGroup 21425] Added subscription to "exact":"chat.test"
2017-01-03T17:48:19.4958000 debug      [TestInternalClient 21425] Client onMessage: [ThruwayMessageSubscribedMessage]
2017-01-03T17:48:19.4965720 info       [ThruwayPeerRouter 21425] Starting loop
2017-01-03T17:48:25.8358310 debug      [ThruwayTransportRatchetTransportProvider 21425] RatchetTransportProvider::onOpen
2017-01-03T17:48:25.9890380 debug      [ThruwayTransportRatchetTransportProvider 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       [ThruwayRealmManager 21425] Got prehello...
2017-01-03T17:48:26.0515670 debug      [ThruwayTransportRatchetTransportProvider 21425] onMessage: ([32,2358366231642639,{},"chat.test"])
2017-01-03T17:48:26.0517410 debug      [ThruwaySubscriptionSubscriptionGroup 21425] Added subscription to "exact":"chat.test"
2017-01-03T17:48:26.0519070 debug      [ThruwayTransportRatchetTransportProvider 21425] onMessage: ([32,8132914407728460,{},"cmd.test"])
2017-01-03T17:48:26.0520030 debug      [ThruwaySubscriptionSubscriptionGroup 21425] Added subscription to "exact":"cmd.test"
2017-01-03T17:48:34.0115900 debug      [ThruwayTransportRatchetTransportProvider 21425] onMessage: ([16,3263467437225103,{},"chat.test",[{"token":"6b28af4056d650480e726e33986a2e790e34abb1","message":"Write from my chatbox"}]])
2017-01-03T17:48:34.0121500 debug      [TestInternalClient 21425] Client onMessage: [ThruwayMessageEventMessage]

如果你有任何想法…谢谢!:)

class TestInternalClient extends ThruwayPeerClient {
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' ]);
}
}

相关内容

  • 没有找到相关文章

最新更新