-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws-server.php
More file actions
37 lines (31 loc) · 873 Bytes
/
ws-server.php
File metadata and controls
37 lines (31 loc) · 873 Bytes
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
<?php
namespace WebSocket;
require __DIR__ . '/vendor/autoload.php';
try {
$server = new Server([
'port' => 8081,
'timeout' => 200,
'filter' => ['text', 'close'],
]);
} catch (ConnectionException $e) {
echo "> ERROR: {$e->getMessage()}\n";
die();
}
echo "> Listening to port {$server->getPort()}\n";
while (true) {
while ($server->accept()) {
echo "> Accepted on port {$server->getPort()}\n";
while (true) {
$message = $server->receive();
$opcode = $server->getLastOpcode();
if ($message === null) {
echo "> Closing connection\n";
continue 2;
}
echo "> Got '$message' [opcode: {$opcode}]\n";
if ($message) {
$server->text($message);
}
}
}
}