Skip to content

Commit 19dcae3

Browse files
committed
Added examples
1 parent bed48d4 commit 19dcae3

14 files changed

Lines changed: 748 additions & 2 deletions

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,58 @@ Dazzle Socket features:
3030

3131
### Quickstart
3232

33-
TODO
33+
Server file which accepts the range in format of $min-$max and returns randomized number.
34+
35+
```php
36+
$loop = new Loop(new SelectLoop);
37+
$server = new SocketListener('tcp://127.0.0.1:2080', $loop);
38+
39+
$server->on('connect', function($server, SocketInterface $client) {
40+
$client->write("Hello!\n");
41+
$client->write("Welcome to Dazzle server!\n");
42+
$client->write("Tell me a range and I will randomize a number for you!\n\n");
43+
44+
$client->on('data', function(SocketInterface $client, $data) use(&$buffer) {
45+
$client->write("Your number is: " . rand(...explode('-', $data)));
46+
});
47+
});
48+
49+
$loop->onStart(function() use($server) {
50+
$server->start();
51+
});
52+
$loop->start();
53+
```
54+
55+
Client file which sends the $min-$max format to the above server and gets the response.
56+
57+
```php
58+
$loop = new Loop(new SelectLoop);
59+
$socket = new Socket('tcp://127.0.0.1:2080', $loop);
60+
61+
$socket->on('data', function($socket, $data) {
62+
printf("%s", $data);
63+
});
64+
$socket->write('1-100');
65+
66+
$loop->start();
67+
```
3468

3569
### Additional
3670

37-
TODO
71+
Additional examples can be found in [example](https://github.com/dazzle-php/socket/tree/master/example) directory. Below is the list of provided examples as a reference and preferred consumption order:
72+
73+
- [Quickstart](https://github.com/dazzle-php/socket/blob/master/example/events_quickstart.php)
74+
- [Using socket client](https://github.com/dazzle-php/socket/blob/master/example/socket_only_client.php)
75+
- [Using socket server](https://github.com/dazzle-php/socket/blob/master/example/socket_only_server.php)
76+
- [Creating TCP IPv4 client-server connection](https://github.com/dazzle-php/socket/blob/master/example/socket_conn_tcp.php)
77+
- [Creating TCP IPv6 client-server connection](https://github.com/dazzle-php/socket/blob/master/example/socket_conn_tcp_ipv6.php)
78+
- [Creating UNIX socket client-server connection](https://github.com/dazzle-php/socket/blob/master/example/socket_conn_unix.php)
79+
- [Getting connection info](https://github.com/dazzle-php/socket/blob/master/example/socket_info.php)
80+
- [Using secure SSL socket client](https://github.com/dazzle-php/socket/blob/master/example/socket_ssl_only_client.php)
81+
- [Using secure SSL socket server](https://github.com/dazzle-php/socket/blob/master/example/socket_ssl_only_server.php)
82+
- [Creating secure SSL client-server connection](https://github.com/dazzle-php/socket/blob/master/example/socket_ssl.php)
83+
84+
If any of the above examples has left you confused, please take a look in the [tests](https://github.com/dazzle-php/socket/tree/master/test) directory as well.
3885

3986
## Requirements
4087

example/bootstrap/autoload.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../../vendor/autoload.php';
4+
5+
$options = [
6+
'mode' => 'client',
7+
];
8+
9+
foreach ($argv as $argIndex=>$argVal)
10+
{
11+
if ($argIndex && preg_match('#^--([^=]+)=(.+)$#si', $argVal, $matches) && $matches)
12+
{
13+
$options[$matches[1]] = $matches[2];
14+
}
15+
}
16+
17+
foreach ($options as $optionKey=>$optionVal)
18+
{
19+
$$optionKey = strtolower($optionVal);
20+
}

example/socket_conn_tcp.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of using Socket component to send messages between client and listeners with IPv4.
8+
*
9+
* ---------------------------------------------------------------------------------------------------------------------
10+
* USAGE
11+
* ---------------------------------------------------------------------------------------------------------------------
12+
* To run this example in CLI from project root use following syntax
13+
*
14+
* $> php ./example/socket_conn_tcp.php
15+
*
16+
* Following flags are supported to test example:
17+
*
18+
* --mode : define whether socket or client should be created, default: standard, supported: [ client, server ]
19+
*
20+
* Remember that to see working communication, you need to create server and at least one client!
21+
*
22+
* $> php ./example/socket_conn_tcp.php --mode=server
23+
* $> php ./example/socket_conn_tcp.php --mode=client
24+
*
25+
* ---------------------------------------------------------------------------------------------------------------------
26+
*/
27+
28+
$mode = 'client';
29+
30+
require_once __DIR__ . '/bootstrap/autoload.php';
31+
32+
use Dazzle\Loop\Model\SelectLoop;
33+
use Dazzle\Loop\Loop;
34+
use Dazzle\Socket\Socket;
35+
use Dazzle\Socket\SocketInterface;
36+
use Dazzle\Socket\SocketListener;
37+
38+
$loop = new Loop(new SelectLoop);
39+
40+
if ($mode === 'server')
41+
{
42+
$server = new SocketListener('tcp://127.0.0.1:2080', $loop);
43+
44+
$server->on('connect', function($server, SocketInterface $client) {
45+
printf("New connection #%s from %s!\n", $res = $client->getResourceId(), $client->getLocalAddress());
46+
47+
$client->on('data', function($client, $data) use(&$buffer) {
48+
printf("Received message=\"%s\"\n", $data);
49+
});
50+
$client->on('close', function() use($res) {
51+
printf("Closed connection #$res\n");
52+
});
53+
});
54+
$server->start();
55+
}
56+
57+
if ($mode === 'client')
58+
{
59+
$socket = new Socket('tcp://127.0.0.1:2080', $loop);
60+
$socket->on('close', function() use($loop) {
61+
printf("Server has closed the connection!\n");
62+
$loop->stop();
63+
});
64+
65+
$loop->addPeriodicTimer(1, function() use($socket) {
66+
$socket->write('Hello World!');
67+
});
68+
}
69+
70+
$loop->start();

example/socket_conn_tcp_ipv6.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of using Socket component to send messages between client and listeners with IPv6.
8+
*
9+
* ---------------------------------------------------------------------------------------------------------------------
10+
* USAGE
11+
* ---------------------------------------------------------------------------------------------------------------------
12+
* To run this example in CLI from project root use following syntax
13+
*
14+
* $> php ./example/socket_conn_tcp_ipv6.php
15+
*
16+
* Following flags are supported to test example:
17+
*
18+
* --mode : define whether socket or client should be created, default: standard, supported: [ client, server ]
19+
*
20+
* Remember that to see working communication, you need to create server and at least one client!
21+
*
22+
* $> php ./example/socket_conn_tcp_ipv6.php --mode=server
23+
* $> php ./example/socket_conn_tcp_ipv6.php --mode=client
24+
*
25+
* ---------------------------------------------------------------------------------------------------------------------
26+
*/
27+
28+
$mode = 'client';
29+
30+
require_once __DIR__ . '/bootstrap/autoload.php';
31+
32+
use Dazzle\Loop\Model\SelectLoop;
33+
use Dazzle\Loop\Loop;
34+
use Dazzle\Socket\Socket;
35+
use Dazzle\Socket\SocketInterface;
36+
use Dazzle\Socket\SocketListener;
37+
38+
$loop = new Loop(new SelectLoop);
39+
40+
if ($mode === 'server')
41+
{
42+
$server = new SocketListener('tcp://[::1]:2080', $loop);
43+
44+
$server->on('connect', function($server, SocketInterface $client) {
45+
printf("New connection #%s from %s!\n", $res = $client->getResourceId(), $client->getLocalAddress());
46+
47+
$client->on('data', function($client, $data) use(&$buffer) {
48+
printf("Received message=\"%s\"\n", $data);
49+
});
50+
$client->on('close', function() use($res) {
51+
printf("Closed connection #$res\n");
52+
});
53+
});
54+
$server->start();
55+
}
56+
57+
if ($mode === 'client')
58+
{
59+
$socket = new Socket('tcp://[::1]:2080', $loop);
60+
$socket->on('close', function() use($loop) {
61+
printf("Server has closed the connection!\n");
62+
$loop->stop();
63+
});
64+
65+
$loop->addPeriodicTimer(1, function() use($socket) {
66+
$socket->write('Hello World!');
67+
});
68+
}
69+
70+
$loop->start();

example/socket_conn_unix.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of using Socket component to send messages between client and listeners with UNIX
8+
* sockets.
9+
*
10+
* ---------------------------------------------------------------------------------------------------------------------
11+
* USAGE
12+
* ---------------------------------------------------------------------------------------------------------------------
13+
* To run this example in CLI from project root use following syntax
14+
*
15+
* $> php ./example/socket_conn_unix.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_conn_unix.php --mode=server
24+
* $> php ./example/socket_conn_unix.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('unix://' . __DIR__ . '/mysock.sock', $loop);
44+
45+
$server->on('connect', function($server, SocketInterface $client) {
46+
printf("New connection #%s from %s!\n", $res = $client->getResourceId(), $client->getLocalAddress());
47+
48+
$client->on('data', function($client, $data) use(&$buffer) {
49+
printf("Received message=\"%s\"\n", $data);
50+
});
51+
$client->on('close', function() use($res) {
52+
printf("Closed connection #$res\n");
53+
});
54+
});
55+
$server->start();
56+
}
57+
58+
if ($mode === 'client')
59+
{
60+
$socket = new Socket('unix://' . __DIR__ . '/mysock.sock', $loop);
61+
$socket->on('close', function() use($loop) {
62+
printf("Server has closed the connection!\n");
63+
$loop->stop();
64+
});
65+
66+
$loop->addPeriodicTimer(1, function() use($socket) {
67+
$socket->write('Hello World!');
68+
});
69+
}
70+
71+
$loop->start();

example/socket_info.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)