-
Notifications
You must be signed in to change notification settings - Fork 5
/
socket_only_server.php
43 lines (35 loc) · 1.57 KB
/
socket_only_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
<?php
/**
* ---------------------------------------------------------------------------------------------------------------------
* DESCRIPTION
* ---------------------------------------------------------------------------------------------------------------------
* This file contains the example of using Socket clients with secure SSL connection.
*
* ---------------------------------------------------------------------------------------------------------------------
* USAGE
* ---------------------------------------------------------------------------------------------------------------------
* To run this example in CLI from project root use following syntax
*
* $> php ./example/socket_only_server.php
*
* ---------------------------------------------------------------------------------------------------------------------
*/
require_once __DIR__ . '/bootstrap/autoload.php';
use Dazzle\Loop\Model\SelectLoop;
use Dazzle\Loop\Loop;
use Dazzle\Socket\SocketInterface;
use Dazzle\Socket\SocketListener;
$loop = new Loop(new SelectLoop);
$server = new SocketListener('tcp://127.0.0.1:2080', $loop);
$server->on('connect', function($server, SocketInterface $client) {
printf("New secure connection #%s from %s!\n", $res = $client->getResourceId(), $client->getLocalAddress());
$client->on('data', function(SocketInterface $client, $data) use(&$buffer) {
printf("%s\n", $data);
$client->write('secret answer!');
});
$client->on('close', function() use($res) {
printf("Closed connection #$res\n");
});
});
$server->start();
$loop->start();