2017-01-03 8 views
2

投票アプリケーションにwebsocketを使用したいと思います。 1つはチャットチャンネルであり、もう1つはコマンドチャンネルです。質問に関する情報、利用可能な回答、カウントダウンなどが移動します。Thruwayでサーバー側のメッセージを解析する方法

クライアント側では、ライブラリとしてAutobahn|JSを使用します。私は自分の2つのチャンネルを簡単に購読することができます(サーバーログで見ることができます)

サーバ側では、WAMP v2準拠のため、私はThruwayを使用します。

私の問題:私はどのように私は各トピックでそれらを放送するためにサーバー側でメッセージをフックすることができますか分かりません。簡単ですが、私はどんな解決策も見つけることができます。

いくつかのコードが

...私はそれが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(); 

Serverのログ

[email protected]:~/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] 

あなたが任意のアイデアを持っている場合は...ありがとう! :)

答えて

2
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' ]); 
    } 
} 
+0

ありがとうございました!静的なチャンネル名で動作します。しかし、ダイナミックな名前でいくつかのチャットルームを管理したい場合、どうしたらいいですか? ログにはクライアントがチャンネルに登録している時刻が表示されます。メッセージを再生するために内部クライアントに自動的に登録するようにしたいと考えています。 これを行う方法はありますか? – Guillaume

+0

@Guillaume - 私はすべてのトピックを同時に購読するものを追加しました。これがこれを処理する方法の1つです。 – mbonneau

+0

ありがとう、これは魅力的なように働いています!私はあなたの助けなしにそれを見つけることができませんでした;) – Guillaume

関連する問題