gpt4 book ai didi

php - 如何使用 Thruway 在服务器端解析消息

转载 作者:行者123 更新时间:2023-12-04 16:13:09 24 4
gpt4 key购买 nike

我想将 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服务器
<?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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com