-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocket_only_client.php
42 lines (34 loc) · 1.34 KB
/
socket_only_client.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
<?php
/**
* ---------------------------------------------------------------------------------------------------------------------
* DESCRIPTION
* ---------------------------------------------------------------------------------------------------------------------
* This file contains the example of using Socket clients.
*
* ---------------------------------------------------------------------------------------------------------------------
* USAGE
* ---------------------------------------------------------------------------------------------------------------------
* To run this example in CLI from project root use following syntax
*
* $> php ./example/socket_only_client.php
*
* ---------------------------------------------------------------------------------------------------------------------
*/
require_once __DIR__ . '/bootstrap/autoload.php';
use Dazzle\Loop\Model\SelectLoop;
use Dazzle\Loop\Loop;
use Dazzle\Socket\Socket;
use Dazzle\Socket\SocketInterface;
$loop = new Loop(new SelectLoop);
$socket = new Socket('tcp://127.0.0.1:2080', $loop);
$socket->on('data', function(SocketInterface $client, $data) use(&$buffer) {
printf("%s\n", $data);
$client->close();
});
$socket->on('close', function() use($loop) {
$loop->stop();
});
$loop->onStart(function() use($socket) {
$socket->write('Hello!');
});
$loop->start();