2017-03-03 3 views
0

Wie kann ich speichern/Formulardaten Griff, wenn der Benutzer die nächsten Schritte tun:Websocket Bündel/Ratchet in symfony 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. 

ich diese Daten verwenden wollen zwei Benutzer für einen Chatraum zu koppeln, so dass diese Daten sein sollte sichtbar für alle Benutzeranfragen.

Ich bin total nicht wissen, welche Aufgabe ich sollte zu verwenden, das zu tun :(

jemand Sie wissen, wie dies zu tun? Ich bin mit Gos WebsocketBundle für symfony.

Antwort

0

II kam up mit Art und Weise, das zu tun. ich bin sicher, dass es nicht richtig ist, aber funktioniert.

ich globale Array für alle Verbindungen

private $globals = array(); 

Und jetzt mache ich s gemacht omething wie folgt aus:

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

Jetzt bin ich gebrauchte Perodic Timer wie folgt aus:

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); 
       } 
      }); 
      } 
} 
  • FAZIT:
    • Haupt-ID für erfassen Personen aktuellen websocket Session-ID ist.
    • globaler Array, in dem KEY aktuelle ws Session-ID ist Formulardaten Umgang
    • Jede Verbindung neue Perodic Timer startet als "getPair-" + aktuelle ws Session-ID
    • Bei disconnect Ich überprüfe, dass die globale Array mit Strom ws Sitzungs-ID ist festgelegt. Wenn ja, zerstöre.