2014-01-16 51 views
2

即時通訊工作在基於Symfony 2.2.11的PHP項目上,我安裝了與以下教程http://socketo.me/docs/install相關的socketo,以使我的聊天腳本正常工作。PHP WebSocket ZMQ - 聊天操作 - 發送數據給特定用戶

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 
        } 
       ); 

如此完美的工作everytings,當我發送一個新的消息,它去到DB那麼它的土地在頁面上的聊天。

問題: 數據的土地哪裏的JS腳本,其結果是,所有用戶都可以得到相同的記錄信息

問: 我怎樣才能使數據的土地在特定用戶頁面?

謝謝

+0

你有沒有看這個答案http://stackoverflow.com/questions/17583903/how-to-get-the-connection-object-of-a-specific-user# 17737332 – mattexx

+0

是的,我做了,我們不是通過相同的方法工作,他的聊天類擴展MessageComponentInterface引用http://socketo.me/docs/hello-world和我的聊天類擴展WampServerInterface引用http://socketo.me/docs/push –

回答

3

您是在後端側使用Ratchet,對不對?

所以,在這裏你有情況,你需要很好的例子:(!不是資源ID的集合)

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

你應該保持內部$clients財產您的客戶端連接。因此,您可以從此集合中選擇一個元素,並僅向此客戶端發送消息。

例子:

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 
} 
+2

剛剛嘗試過,它不起作用,仍然將它廣播到打開接收頁面的所有瀏覽器頁面。我想發送郵件到特定的用戶名帳戶(Symfony),例如,如果可能的話 –

+2

其實我正在研究這個http://socketo.me/docs/push精確地,謝謝 –

+1

您能請在http:/ /stackoverflow.com/questions/29576033/send-a-value-from-browser-to-websocket-server-while-opening-connection,你當然可以提供幫助,非常感謝 –