Skip to content

Commit bc0627c

Browse files
committed
Merge branch '2.0'
2 parents 4708cad + 5539b3e commit bc0627c

File tree

25 files changed

+236
-172
lines changed

25 files changed

+236
-172
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"symfony/console": "^3.2"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "^5.0"
20+
"phpunit/phpunit": "^5.0",
21+
"fzaninotto/faker": "^1.6"
2122
},
2223
"autoload": {
2324
"psr-4": {

docs/zh_CN/2-1-server.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DemoServer extends TCP
2222
{
2323
public function doWork(swoole_server $server, $fd, $data, $from_id)
2424
{
25-
return 'hello tcp';
25+
$server->send($fd, $data);
2626
}
2727
}
2828

@@ -41,9 +41,9 @@ use \FastD\Swoole\Server\UDP;
4141

4242
class DemoServer extends UDP
4343
{
44-
public function doPacket(swoole_server $server, $data, $client_info)
44+
public function doPacket(swoole_server $server, $data, $clientInfo)
4545
{
46-
return 'hello udp';
46+
$server->sendto($clientInfo['address'], $clientInfo['port'], $data);
4747
}
4848
}
4949

docs/zh_CN/2-2-client.md

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,32 @@
22

33
首先得必须说明一下什么是客户端,所谓客户端,就是咱们所指的 "消费者",服务器,也有可能是客户端。可以这么理解,只要是发起请求到另外一段获取获取的,即可视为客户端。因此有时候咱们的服务器,也是其中一个客户端。
44

5-
客户端最后通过 `resolve` 进行调用。
6-
7-
### 客户端命令行
8-
9-
> 1.1.0 新增
10-
11-
当用户输入 (quit/exit) 的时候,客户端自动退出当前循环。
12-
13-
默认数据为: `Hello World`
14-
15-
```php
16-
$ php swoole [host] [port]
17-
```
18-
19-
效果:
20-
21-
```
22-
➜ swoole (1.1) ✗ php swoole 11.11.11.22 9527
23-
Please enter the send data.(default: Hello World, Enter (exit/quit) can be exit console.):
24-
Receive: Hello World
25-
Please enter the send data.(default: Hello World, Enter (exit/quit) can be exit console.): exit
26-
```
27-
285
### 同步客户端
296

307
同步客户端是最传统的一种方式,也是最容易上手的,整个过程都是阻塞的。
318

329
```php
33-
use FastD\Swoole\Client\Sync\SyncClient;
10+
use FastD\Swoole\Client;
3411

3512

36-
$client = new SyncClient('tcp://127.0.0.1:9527');
13+
$client = new Client('tcp://127.0.0.1:9527');
3714

38-
$client
39-
->connect(function ($client) {
40-
$client->send('hello world');
41-
})
42-
->receive(function ($client, $data) {
43-
echo $data . PHP_EOL;
44-
$client->close();
45-
})
46-
->resolve()
47-
;
15+
$client->send();
4816
```
4917

5018
### 异步客户端
5119

5220
不管是同步还是异步客户端,每个方法都是一个回调,统一客户端的写法,避免造成多种操作方式的,造成混淆。
5321

54-
值得注意的是,异步客户端需要对每个操作进行回调处理。
55-
56-
```php
57-
use FastD\Swoole\Client\Async\AsyncClient;
58-
59-
60-
$client = new AsyncClient('tcp://127.0.0.1:9527');
61-
62-
$client
63-
->connect(function ($client) {
64-
$client->send('hello world');
65-
})
66-
->receive(function ($client, $data) {
67-
echo $data . PHP_EOL;
68-
})
69-
->error(function ($client) {
70-
print_r($client);
71-
})
72-
->close(function ($client) {})
73-
->resolve()
74-
;
75-
```
76-
77-
### Socket 客户端
78-
79-
Socket 客户端是基于 PHP 内部的 sockets 扩展实现,因此使用该客户端前先要确保 sockets 扩展已经正常安装。
22+
值得注意的是,异步客户端需要对 `connect`, `receive`, `error`, `close` 进行重写,并且通过 start 进行启动客户端
8023

8124
```php
82-
use FastD\Swoole\Client\Socket;
25+
use FastD\Swoole\Client;
8326

8427

85-
$socket = new Socket('tcp://127.0.0.1:9527');
28+
$client = new Client('tcp://127.0.0.1:9527');
8629

87-
$socket->connect(function (Socket $socket) {
88-
$socket->send('hello world');
89-
})->receive(function ($data) {
90-
echo ($data) . PHP_EOL;
91-
})->resolve();
30+
$client->start();
9231
```
9332

9433
下一节: [进程](2-3-process.md)

docs/zh_CN/3-2-overrite.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 重写回调

docs/zh_CN/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
高级
1818
-------
1919

20-
* [信号量](3-1-singo.md)
20+
* [信号量](3-1-signo.md)
21+
* [重写回调](3-2-overrite.md)
2122

2223
### Support
2324

examples/client/async.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99

1010
include __DIR__ . '/../../vendor/autoload.php';
1111

12-
$client = new \FastD\Swoole\Client('tcp://127.0.0.1:9527');
12+
class Cli extends \FastD\Swoole\Client
13+
{
14+
public function connect(swoole_client $client)
15+
{
16+
$client->send('hello');
17+
}
1318

14-
echo $client->send('hello', true);
19+
public function receive(swoole_client $client, $data)
20+
{
21+
echo $data;
22+
$client->close();
23+
}
24+
}
25+
26+
$client = new Cli('tcp://127.0.0.1:9527', true);
27+
28+
$client->start();

examples/client/tcp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
$client = new \FastD\Swoole\Client('tcp://127.0.0.1:9527');
66

7-
echo $client->send('hello', true);
7+
echo $client->send('hello');
88

99

examples/client/udp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
include __DIR__ . '/../../vendor/autoload.php';
1111

12-
$client = new \FastD\Swoole\Client('tcp://127.0.0.1:9527', SWOOLE_SOCK_UDP);
12+
$client = new \FastD\Swoole\Client('tcp://127.0.0.1:9527', false, false, SWOOLE_SOCK_UDP);
1313

1414
echo $client->send('hello');

examples/coroutine/tcp.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/keepalive/client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
include __DIR__ . '/../../vendor/autoload.php';
1111

12-
$client = new \FastD\Swoole\Client\Sync\TCP('tcp://127.0.0.1:9527');
12+
$client = new \FastD\Swoole\Client('tcp://127.0.0.1:9527', false, true);
1313

1414
for ($i = 0; $i< 10; $i++) {
1515
echo $client->send('hello', false, true) . PHP_EOL;

0 commit comments

Comments
 (0)