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已設置。如果是,請銷燬。