forked from tpunt/pht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pht_websocket.php
57 lines (45 loc) · 1.39 KB
/
pht_websocket.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
require 'vendor/autoload.php';
include 'PhtWSInterface.php';
include 'PhtWSThread.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use PhtEvfdQ\PhtWSInterface;
$channelCount = 5;
$threads = [];
$queu_arr = [];
$queu_ret = new pht\Queue();
for ($i = 0; $i < $channelCount; $i++) {
$queu_arr[$i] = new pht\Queue();
$threads[$i] = new pht\Thread();
$threads[$i]->addClassTask(PhtWSThread::class, $queu_arr[$i], $queu_ret);
$threads[$i]->start();
}
$loop = React\EventLoop\Factory::create();
$websockClients = new \SplObjectStorage;
$readq = new React\Stream\ReadableResourceStream($queu_ret->eventfd(true, false), $loop);
$readq->on('data', function ($data/*not used*/) use ($websockClients, $queu_ret) {
while ($queu_ret->size()) {
$queu_ret->lock();
$ev = $queu_ret->pop();
$queu_ret->unlock();
$ev = json_decode($ev);
foreach ($websockClients as $client) {
if ($ev->cid == $client->resourceId) {
$client->send($ev->msg);
}
}
}
});
$phtWSInterface = new PhtWSInterface($websockClients, $queu_arr);
$webSock = new React\Socket\Server('0.0.0.0:9000', $loop);
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
$phtWSInterface
)
),
$webSock
);
$loop->run();