2017-03-03 48 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已设置。如果是,请销毁。