forked from morozovsk/websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsocketTest.php
52 lines (42 loc) · 1.86 KB
/
WebsocketTest.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
<?php
class WebsocketTest
{
public $clients = array();
public function __construct($config) {
$this->config = $config;
}
public function start() {
for ($i=0, $j=0; $i<=1500; $i++) {
$client = @stream_socket_client($this->config['websocket'], $errorNumber, $errorString, 1);
if ($client) {
fwrite($client, "GET / HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: tQXaRIOk4sOhgoq7SBs43g==\r\nSec-WebSocket-Version: 13\r\n\r\n");
$this->clients[$i] = $client;
if ($i && $i % 100 == 0) echo "success: $i, failure: $j\r\n";
} else {
$i--;
$j++;
if ($j && $j % 100 == 0) echo "success: $i, failure: $j\r\n";
}
}
while (true) {
//подготавливаем массив всех сокетов, которые нужно обработать
$read = $this->clients;
//$read[] = $service;
stream_select($read, $write, $except, null);//обновляем массив сокетов, которые можно обработать
if ($read) {//пришли данные от подключенных клиентов
foreach ($read as $client) {
$data = fread($client, 100000);
if (!$data) { //соединение было закрыто
unset($this->clients[array_search($client, $this->clients)]);
@fclose($client);
continue;
}
//echo $data . "\n";
/*if (!rand(0, 10000)) {
fwrite($this->clients[rand(0, 1000)], $data);
}*/
}
}
}
}
}