-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpenSwoole-WebSocket-Server.php
68 lines (58 loc) · 1.92 KB
/
OpenSwoole-WebSocket-Server.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
58
59
60
61
62
63
64
65
66
67
68
<?php
use Swoole\WebSocket\Server;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;
////////////////////////////////////////////////////////////////////////////////
$server = new Server("127.0.0.1", 8888);
$server->set([
//
// important for allowing larger files to be uploaded:
//
'package_max_length' => 8 * 1024 * 1024,
]);
//--------------------------------------------------------------
$server->on("Start", function(Server $server)
{
echo "Swoole WebSocket Server is started at wss://127.0.0.1:9502\n";
});
//--------------------------------------------------------------
$server->on('Open', function(Server $server, Swoole\Http\Request $request)
{
echo "connection open: {$request->fd}\n";
print_r($server->getClientInfo($request->fd));
});
//--------------------------------------------------------------
$server->on('Message', function(Server $server, Frame $frame)
{
if ('!' == $frame->data[0]) {
// we're only testing binary uploads here
list($jsonMsg, $fileData) = explode("\r\n\r\n", $frame->data, 2);
$jsonMsg = substr($jsonMsg, 1); // trim '!' here vs on the larger file
$jsonMsg = json_decode($jsonMsg);
echo "\n\n";
print_r("frame length: ".strlen($frame->data));
print_r(array_keys(get_object_vars($frame)));
print_r($jsonMsg);
if ($jsonMsg->size === strlen($fileData)) {
file_put_contents(__DIR__.'/uploads/'.$jsonMsg->name, $fileData);
}
else {
echo "\nfile byte length reported from browser unequal to data received\n";
}
}
else {
// ...handle your other message types
}
});
//--------------------------------------------------------------
$server->on('Close', function(Server $server, int $fd)
{
echo "connection close: {$fd}\n";
});
//--------------------------------------------------------------
$server->on('Disconnect', function(Server $server, int $fd)
{
echo "connection disconnect: {$fd}\n";
});
//--------------------------------------------------------------
$server->start();