|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------------------------------------------------------- |
| 5 | + * DESCRIPTION |
| 6 | + * --------------------------------------------------------------------------------------------------------------------- |
| 7 | + * This file contains the example of using methods created for getting the information about the incoming/outcoming |
| 8 | + * socket connections. |
| 9 | + * |
| 10 | + * --------------------------------------------------------------------------------------------------------------------- |
| 11 | + * USAGE |
| 12 | + * --------------------------------------------------------------------------------------------------------------------- |
| 13 | + * To run this example in CLI from project root use following syntax |
| 14 | + * |
| 15 | + * $> php ./example/socket_info.php |
| 16 | + * |
| 17 | + * Following flags are supported to test example: |
| 18 | + * |
| 19 | + * --mode : define whether socket or client should be created, default: standard, supported: [ client, server ] |
| 20 | + * |
| 21 | + * Remember that to see working communication, you need to create server and at least one client! |
| 22 | + * |
| 23 | + * $> php ./example/socket_info.php --mode=server |
| 24 | + * $> php ./example/socket_info.php --mode=client |
| 25 | + * |
| 26 | + * --------------------------------------------------------------------------------------------------------------------- |
| 27 | + */ |
| 28 | + |
| 29 | +$mode = 'client'; |
| 30 | + |
| 31 | +require_once __DIR__ . '/bootstrap/autoload.php'; |
| 32 | + |
| 33 | +use Dazzle\Loop\Model\SelectLoop; |
| 34 | +use Dazzle\Loop\Loop; |
| 35 | +use Dazzle\Socket\Socket; |
| 36 | +use Dazzle\Socket\SocketInterface; |
| 37 | +use Dazzle\Socket\SocketListener; |
| 38 | + |
| 39 | +$loop = new Loop(new SelectLoop); |
| 40 | + |
| 41 | +if ($mode === 'server') |
| 42 | +{ |
| 43 | + $server = new SocketListener('tcp://127.0.0.1:2080', $loop); |
| 44 | + |
| 45 | + $server->on('connect', function($server, SocketInterface $client) { |
| 46 | + |
| 47 | + printf("%s\n", str_repeat('-', 42)); |
| 48 | + printf("%s\n", 'Client info:'); |
| 49 | + printf("%s\n", str_repeat('-', 42)); |
| 50 | + printf("%-20s%s\n", 'Resource ID:', '#' . $client->getResourceId()); |
| 51 | + printf("%-20s%s\n", 'Local endpoint:', $client->getLocalEndpoint()); |
| 52 | + printf("%-20s%s\n", 'Local protocol:', $client->getLocalProtocol()); |
| 53 | + printf("%-20s%s\n", 'Local address:', $client->getLocalAddress()); |
| 54 | + printf("%-20s%s\n", 'Local host:', $client->getLocalHost()); |
| 55 | + printf("%-20s%s\n", 'Local port:', $client->getLocalPort()); |
| 56 | + printf("%-20s%s\n", 'Remote endpoint:', $client->getRemoteEndpoint()); |
| 57 | + printf("%-20s%s\n", 'Remote protocol:', $client->getRemoteProtocol()); |
| 58 | + printf("%-20s%s\n", 'Remote address:', $client->getRemoteAddress()); |
| 59 | + printf("%-20s%s\n", 'Remote host:', $client->getRemoteHost()); |
| 60 | + printf("%-20s%s\n", 'Remote port:', $client->getRemotePort()); |
| 61 | + printf("%s\n", str_repeat('-', 42)); |
| 62 | + |
| 63 | + $client->close(); |
| 64 | + }); |
| 65 | + $server->start(); |
| 66 | +} |
| 67 | + |
| 68 | +if ($mode === 'client') |
| 69 | +{ |
| 70 | + $socket = new Socket('tcp://127.0.0.1:2080', $loop); |
| 71 | + $socket->on('close', function() use($loop) { |
| 72 | + $loop->stop(); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +$loop->start(); |
0 commit comments