forked from tpunt/pht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhtWSInterface.php
38 lines (30 loc) · 1.03 KB
/
PhtWSInterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
namespace PhtEvfdQ;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class PhtWSInterface implements MessageComponentInterface {
protected $clients;
protected $total_recv = 0;
protected $total_resp = 0;
protected $queu_arr;
public function __construct ($clients, $queu_arr) {
$this->clients = $clients;
$this->queu_arr = $queu_arr;
}
public function onOpen (ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage (ConnectionInterface $from, $msg) {
$msg = json_decode($msg);
$ev = json_encode(['msg' => $msg->data, 'cid' => $from->resourceId]);
$this->queu_arr[$msg->channel]->lock();
$this->queu_arr[$msg->channel]->push($ev);
$this->queu_arr[$msg->channel]->unlock();
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}