-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathbinary-event.php
More file actions
69 lines (63 loc) · 2.13 KB
/
Copy pathbinary-event.php
File metadata and controls
69 lines (63 loc) · 2.13 KB
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
69
<?php
/**
* This file is part of the Elephant.io package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright ElephantIO
* @copyright Wisembly
* @license http://www.opensource.org/licenses/MIT-License MIT License
*/
require __DIR__ . '/common.php';
$namespace = 'binary-event';
$event = 'test-binary';
$event_attachment = 'test-binary-attachment';
$logger = setup_logger();
if (false === ($content = file_get_contents(__DIR__ . '/../../test/Websocket/data/payload-100k.txt'))) {
echo "Payload file is not found!\n";
exit(1);
}
if (false === ($payload = fopen('php://memory', 'w+'))) {
echo "Unable to create payload resource!\n";
exit(1);
}
// create 2MB binary payload
$n = 20;
for ($i = 0; $i < $n; $i++) {
fwrite($payload, $content);
}
$size = 512 * 1024; // 512k
foreach ([
'websocket' => ['transport' => 'websocket'],
'polling' => ['transports' => 'polling']
] as $transport => $options) {
echo sprintf("Sending binary data using %s transport...\n", $transport);
$client = setup_client($namespace, $logger, $options);
// send big payload as parts
$hash = uniqid();
fseek($payload, 0);
while (!feof($payload)) {
if (false === $part = fread($payload, $size)) {
throw new Error('Unable to read attachment part!');
}
if (false === $res = fopen('php://memory', 'r+')) {
throw new Error('Unable to allocate attachment resource!');
}
fwrite($res, $part);
if ($client->emit($event_attachment, ['hash' => $hash, 'content' => $res])) {
if (is_object($packet = $client->wait($event_attachment))) {
if (!$packet->data['success']) {
throw new Error('Send attachment part failed!');
}
}
} else {
throw new Error('Unable to send attachment part!');
}
}
$client->emit($event, ['hash' => $hash]);
if (is_object($retval = $client->wait($event))) {
echo sprintf("Got a reply: %s\n", $retval->inspect());
}
$client->disconnect();
}