0

Ratchet と Autobahn.js を使用しています。サブスクライブ時にユーザー検証を行いたいので、セッション キーを Ratchet WAMP サーバーに渡す必要があります。サブスクライブイベントでサーバーにデータを渡す方法を教えてください。

4

2 に答える 2

4

認証について話しているのではなく、サーバーとの接続が既に確立されていると思います。

クライアントからセッション ID を渡す必要はありません。WAMP がそれを処理します。サブスクライブ時に渡すことができる唯一の情報はトピックです。

PHP側では、検証に使用できるセッションIDにアクセスできます。

public function onSubscribe(ConnectionInterface $conn, $topic) 
{
    $sessionId = $conn->WAMP->sessionId
}



の解決策: 本当にクライアントからセッション ID を渡す必要がある場合は、次のようにすることができます。

Javascript:

var appSession = null;

ab.connect(
   // The WebSocket URI of the WAMP server
   wsuri,

   // The onconnect handler
   function (session) {
      appSession = session;
   }
);


appSession.call('myValidationChannelForUser', appSession.sessionid(), 'otherValidationParams').then(function(result)
{
    if (result.success)
    {
        console.log('you have been subscribed to xyz..');
    }
}

php:

public function onCall(ConnectionInterface $conn, $id, $fn, array $params) 
{ 
    $sessionId = $conn->WAMP->sessionId;

    if ($fn == 'myValidationChannelForUser')
    {
        // validation...
        // $params[0] == appSession.sessionid() passed from JS
        // $params[1] == otherValidationParams passed from JS

        // validation passed, subcribe to channel
        if (validated)
        {
            $this->onSubscribe(ConnectionInterface $conn, $topic); 

            return $conn->callResult($id, array('success' => 1);
        }
    }
}
于 2014-02-13T18:17:16.737 に答える