2017-03-03 84 views
0

我如何保存/處理的形式數據時,用戶會做下一個步驟:的WebSocket捆/棘輪3

-> Fill form 
-> Submit Form 
-> Join to websocket channel. 
-> Now I need to save formdata for current user in moment when He is joined to room. 

我想用這個數據來配對兩個用戶的一個聊天室,所以這個數據應該是可見於每個用戶的請求。

我完全不知道用來做:(

難道有人知道如何做到這一點?我使用GOS WebsocketBundle爲symfony1.2什麼目的我應該。

回答

0

II來了了的方式來做到這一點。 我敢肯定,這是不正確的方法,但工程。

我做全局數組爲每個連接

private $globals = array(); 

現在我執行s omething這樣的:

public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible) 
{ 
     $user = $this->clientManipulator->getClient($connection); 

     /* Saving current session to global array 
     * $event returns form data sent in json format 
     */ 
     if ($topic->getId() === 'chat/global/waiting-room'){ 
      $this->globals[$connection->WAMP->sessionId] = $event; 
      $this->globals[$connection->WAMP->sessionId]['id'] = $connection->WAMP->sessionId; 
     } 
} 

現在我已經習慣perodic計時器是這樣的:

public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request) 
{  
     ## global data: 
     $topicTimer = $connection->PeriodicTimer; 
     $allMembers = $this->clientManipulator->getAll($topic, true); 
     $user = $this->clientManipulator->getClient($connection); 
     $userSessionId = $connection->WAMP->sessionId; 

     if ($topic->getId() === 'chat/global/waiting-room'){ 
      $userData = $this->globals[$connection->WAMP->sessionId]; 

      ## Set timer every 5 seconds 
      if(!$this->periodicTimer->isPeriodicTimerActive($this, 'getPair-'.$userSessionId)){ 
       $this->periodicTimer->addPeriodicTimer($this, 'getPair-'.$userSessionId, 5, function() use ($userData, $userSessionId, $topic) { 

       ## my own method for pair searching in $this->globals; 
       $pair = $this->findPairForCurrentUser($userSessionId); 
       if(NULL !== $pair) 
       { 
        #Send info to Two people about room and do redirect in jquery. 
        $topic->broadcast(
          array(
           'msg' => 'We did it! Connecting now...', 
           'room' => array(
            'slug' => uniqid("room"), 
            'id' => rand(0,1923) 
           ) 
          ), 
          array(), 
          array(
           $userData['id'], 
           $pair 
          ) 
        ); 

        # Remove perodic timer :) 
        $this->periodicTimer->cancelPeriodicTimer($this, 'getPair-'.$userSessionId); 
       } 
      }); 
      } 
} 
  • 結論:
    • 用於檢測人的主ID是當前的WebSocket會話ID。
    • 全球陣列,其中關鍵是目前的WS會話ID是處理表單數據
    • 每個連接啓動新的名爲「getPair-」 +當前WS會話ID
    • 在斷開我檢查perodic定時器,全球陣列與當前的WS會話ID已設置。如果是,請銷燬。