3

私はSymfony 2.2.11に基づくPHPプロジェクトに取り組んでおり、チャットスクリプトを機能させるために、次のチュートリアルhttp://socketo.me/docs/installに関連するソケットをインストールしました。

ServerCommand.php // WebSocket サーバーを起動するコマンドラインのコード

$oLoop = Factory::create();

    // Listen for the web server to make a ZeroMQ push after an ajax request
    $oContext = new Context($oLoop);
    $oPull = $oContext->getSocket(\ZMQ::SOCKET_PULL);
    // LET IT 127.0.0.1
    $oPull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
    $oPull->on('message', array($oChat, 'onMessage'));

    // Set up our WebSocket server for clients wanting real-time updates
    $oWebSock = new Server($oLoop);
    $oWebSock->listen(7979, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new IoServer(
        new HttpServer(
            new WsServer(
                new WampServer(
                    $oChat
                )
            )
        ),
        $oWebSock
    );

    $oLoop->run();

メッセージがデータベースに追加された後: MessagesController.php

....
// This is our new stuff
        $oContext = new \ZMQContext();
        $oSocket = $oContext->getSocket(\ZMQ::SOCKET_PUSH, 'PushMe');
        $oSocket->connect("tcp://mydomain:5555");

        $aData = array(
            'topic'         => 'message',
            'sUsername'     => $oUserCurrent->getUsername(),
            'sMessage'      => $sMessage
        );

        $oSocket->send(json_encode($aData));
.....

チャットサービス: Chat.php

/**
 * A lookup of all the topics clients have subscribed to
 */
public function onSubscribe(ConnectionInterface $conn, $topic) 
{
    // When a visitor subscribes to a topic link the Topic object in a  lookup array
    $subject = $topic->getId();
    $ip = $conn->remoteAddress;

    if (!array_key_exists($subject, $this->subscribedTopics)) 
    {
        $this->subscribedTopics[$subject] = $topic;
    }

    $this->clients[] = $conn->resourceId;

    echo sprintf("New Connection: %s" . PHP_EOL, $conn->remoteAddress);

}

/**
 * @param string JSON'ified string we'll receive from ZeroMQ
 */
public function onMessage($jData) 
{
    $aData = json_decode($jData, true);

    var_dump($aData);

    if (!array_key_exists($aData['topic'], $this->subscribedTopics)) {
        return;
    }

    $topic = $this->subscribedTopics[$aData['topic']];

    // This sends out everything to multiple users, not what I want!!

    // re-send the data to all the clients subscribed to that category
    $topic->broadcast($aData);
}

データを受け取る JS コード: messages.html.twig :

var conn = new ab.Session(
                    'ws://mydomain:7979' // The host (our Ratchet WebSocket server) to connect to
                  , function() {            // Once the connection has been established
                        conn.subscribe('message', function(topic, data) 
                        {
                            console.log(topic);
                            console.log(data);
                        });
                    }
                  , function() {            // When the connection is closed
                        console.warn('WebSocket connection closed');
                    }
                  , {                       // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
                        'skipSubprotocolCheck': true
                    }
                );

新しいメッセージを送信すると、DB に送られ、チャットのページに表示されます。

問題 : データは JS スクリプトのどこにでも置かれ、その結果、すべてのユーザーが同じ録音メッセージを受け取ることができます。

質問:特定のユーザーページ にデータ ランドを作成するにはどうすればよいですか?

ありがとうございました

4

1 に答える 1

3

Ratchetバックエンド側で使用していますよね?

したがって、ここに必要なケースの非常に良い例があります。

http://socketo.me/docs/hello-world

クライアント接続を$clientsプロパティ内に保持する必要があります (リソース ID のコレクションではありません!)。したがって、このコレクションから 1 つの要素を選択して、このクライアントにのみメッセージを送信できます。

例:

public function onSubscribe(ConnectionInterface $conn, $topic) 
{
    // When a visitor subscribes to a topic link the Topic object in a  lookup array
    $subject = $topic->getId();
    $ip = $conn->remoteAddress;

    if (!array_key_exists($subject, $this->subscribedTopics)) 
    {
        $this->subscribedTopics[$subject] = $topic;
    }

    $this->clients[] = $conn; // you add connection to the collection

    $conn->send("Hello new user!"); // you send a message only to this one user
}
于 2014-01-16T22:02:24.297 に答える