Skip to content

Commit adb01c4

Browse files
committed
update demo examples
1 parent 6820484 commit adb01c4

File tree

7 files changed

+131
-84
lines changed

7 files changed

+131
-84
lines changed

README.md

Lines changed: 97 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ composer require "fastd/swoole:1.0.x-dev" -vvv
4040

4141
[中文文档](docs/readme.md)
4242

43-
## 使用
43+
## 使用
4444

4545
服务继承 `FastD\Swoole\Server`, 实现 `doWork` 方法, 服务器在接收信息 `onReceive` 回调中会调用 `doWork` 方法, `doWork` 方法接受一个封装好的请求对象。
4646

4747
具体逻辑在 `doWork` 方法中实现, `doWork` 方法中返回响应客户端的数据, 格式为: **字符串**
4848

49-
服务器通过 `run` 方法执行, `run` 方法中注入配置, 配置按照 `swoole` 原生扩展参数配置。
49+
Swoole 配置通过实现 `configure` 方法进行配置,具体配置参数请参考: [Swoole 配置选项](http://wiki.swoole.com/wiki/page/274.html)
5050

51-
#### Tcp Server
51+
#### TCP Server
5252

5353
```php
5454
class DemoServer extends \FastD\Swoole\Server\Tcp
@@ -58,31 +58,57 @@ class DemoServer extends \FastD\Swoole\Server\Tcp
5858
echo $data . PHP_EOL;
5959
return 'hello tcp';
6060
}
61+
62+
public function configure()
63+
{
64+
$this->pid('/tmp/swoole.pid');
65+
}
6166
}
6267

63-
DemoServer::run('tcp://127.0.0.1:9527');
68+
DemoServer::createServer('tcp swoole', 'tcp://0.0.0.0:9527')->start();
6469
```
6570

66-
#### Http Server
71+
#### UDP Server
72+
73+
```php
74+
class DemoServer extends \FastD\Swoole\Server\Udp
75+
{
76+
public function doPacket(swoole_server $server, $data, $client_info)
77+
{
78+
echo $data . PHP_EOL;
79+
return 'hello tcp';
80+
}
81+
82+
public function configure()
83+
{
84+
// TODO: Implement configure() method.
85+
}
86+
}
87+
88+
DemoServer::createServer('udp swoole', 'udp://127.0.0.1:9527')->start;
89+
```
90+
91+
#### HTTP Server
6792

6893
同理, `Http` 服务器扩展 `Server` 类, 实现 `doRequest` 方法,实现具体逻辑。
6994

7095
```php
7196
class Http extends \FastD\Swoole\Server\Http
7297
{
73-
public function doRequest(\FastD\Http\SwooleServerRequest $request)
98+
public function doRequest(ServerRequest $serverRequest)
7499
{
75-
$request->cookie->set('name', 'jan');
76-
77-
return new \FastD\Http\JsonResponse([
100+
return new JsonResponse([
78101
'msg' => 'hello world',
79-
], 400, [
80-
'NAME' => "Jan"
81102
]);
82103
}
104+
105+
public function configure()
106+
{
107+
108+
}
83109
}
84110

85-
Http::run('http://0.0.0.0:9527');
111+
Http::createServer('http', 'http://0.0.0.0:9527')->start();
86112
```
87113

88114
目前 Http 服务支持 Session 存储,而 Session 存储是基于浏览器 cookie,或者可以自定义实现存储方式。
@@ -104,65 +130,70 @@ class WebSocket extends \FastD\Swoole\Server\WebSocket
104130
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
105131
$server->push($frame->fd, "this is server");
106132
}
133+
134+
public function configure()
135+
{
136+
// TODO: Implement configure() method.
137+
}
107138
}
108139

109-
WebSocket::run('ws://0.0.0.0:9527');
140+
WebSocket::createServer('ws', 'ws://0.0.0.0:9527')->start();
110141
```
111142

112143
#### 多端口支持
113144

114145
```php
115146
class Server extends \FastD\Swoole\Server\Tcp
116147
{
117-
/**
118-
* @param swoole_server $server
119-
* @param $fd
120-
* @param $data
121-
* @param $from_id
122-
* @return mixed
123-
*/
124148
public function doWork(swoole_server $server, $fd, $data, $from_id)
125149
{
126150
return 'hello server1';
127151
}
152+
153+
public function configure()
154+
{
155+
// TODO: Implement configure() method.
156+
}
128157
}
129158

130159
class Server2 extends \FastD\Swoole\Server\Tcp
131160
{
132-
/**
133-
* @param swoole_server $server
134-
* @param $fd
135-
* @param $data
136-
* @param $from_id
137-
* @return mixed
138-
*/
139161
public function doWork(swoole_server $server, $fd, $data, $from_id)
140162
{
141163
return 'hello server2';
142164
}
165+
166+
public function configure()
167+
{
168+
// TODO: Implement configure() method.
169+
}
143170
}
144171

145-
$server = new Server('tcp://127.0.0.1:9726');
172+
$server = new Server('tcp server', 'tcp://127.0.0.1:9527');
146173

147-
$server->listen(new Server2('tcp://127.0.0.1:9528'));
174+
$server->listen(new Server2('tcp server2', 'tcp://127.0.0.1:9528'));
148175

149176
$server->start();
150177
```
151178

152179
#### 服务管理
153180

154181
```php
155-
use FastD\Swoole\Server\Tcp\TcpServer;
156-
157-
class DemoServer extends TcpServer
182+
class DemoServer extends \FastD\Swoole\Server\Tcp
158183
{
159184
public function doWork(swoole_server $server, $fd, $data, $from_id)
160185
{
186+
echo $data . PHP_EOL;
161187
return 'hello tcp';
162188
}
189+
190+
public function configure()
191+
{
192+
$this->pid('/tmp/swoole.pid');
193+
}
163194
}
164195

165-
$server = new DemoServer('tcp://127.0.0.1:9527');
196+
$server = DemoServer::createServer('tcp swoole', 'tcp://0.0.0.0:9527');
166197

167198
$argv = $_SERVER['argv'];
168199

@@ -191,23 +222,22 @@ switch ($argv[1]) {
191222
所以这里提供监听文件变化来到自动重启服务(建议开发环境中使用)
192223

193224
```php
225+
194226
class DemoServer extends \FastD\Swoole\Server\Tcp
195227
{
196-
/**
197-
* @param swoole_server $server
198-
* @param $fd
199-
* @param $data
200-
* @param $from_id
201-
* @return mixed
202-
*/
203228
public function doWork(swoole_server $server, $fd, $data, $from_id)
204229
{
205230
return 'hello tcp';
206231
}
207-
}
208232

209-
$server = new DemoServer('tcp://0.0.0.0:9527');
233+
public function configure()
234+
{
235+
// TODO: Implement configure() method.
236+
}
237+
}
210238

239+
$server = new DemoServer('watch server', 'tcp://0.0.0.0:9527');
240+
// $server = DemoServer::createServer('watch server', 'tcp://0.0.0.0:9527');
211241
$server->watch([__DIR__ . '/listen_files']);
212242
```
213243

@@ -253,9 +283,7 @@ $client
253283
#### Process
254284

255285
```php
256-
use FastD\Swoole\Process;
257-
258-
$process = new Process(function () {
286+
$process = new Process('single', function () {
259287
timer_tick(1000, function ($id) {
260288
static $index = 0;
261289
$index++;
@@ -266,8 +294,6 @@ $process = new Process(function () {
266294
});
267295
});
268296

269-
$process->name('process');
270-
271297
$process->start();
272298

273299
$process->wait(function ($ret) {
@@ -278,9 +304,7 @@ $process->wait(function ($ret) {
278304
#### Multi Process
279305

280306
```php
281-
use FastD\Swoole\Process;
282-
283-
$process = new Process(function () {
307+
$process = new Process('multi', function () {
284308
timer_tick(1000, function ($id) {
285309
static $index = 0;
286310
$index++;
@@ -291,13 +315,34 @@ $process = new Process(function () {
291315
});
292316
});
293317

294-
$process->name('process');
295-
296318
$process->fork(5);
297319

298320
$process->wait(function ($ret) {
299321
echo 'PID: ' . $ret['pid'] . PHP_EOL;
300322
});
301323
```
302324

325+
#### Queue
326+
327+
```php
328+
$queue = new \FastD\Swoole\Queue('queue', function ($worker) {
329+
while (true) {
330+
$recv = $worker->pop();
331+
echo "From Master: $recv\n";
332+
}
333+
});
334+
335+
$queue->start();
336+
337+
while (true) {
338+
$queue->push('hello');
339+
sleep(1);
340+
}
341+
342+
343+
$queue->wait(function ($ret) {
344+
echo 'PID: ' . $ret['pid'];
345+
});
346+
```
347+
303348
# License MIT

examples/listen_files/demo2.html

Whitespace-only changes.

examples/listen_files/demo3.html

Whitespace-only changes.

examples/udp/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function configure()
3030
}
3131
}
3232

33-
return DemoServer::createServer('udp swoole', 'tcp://127.0.0.1:9527');
33+
return DemoServer::createServer('udp swoole', 'udp://127.0.0.1:9527');
3434

3535
/**
3636
* 以上写法和以下写法效果一致

examples/watch.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,18 @@ public function doWork(swoole_server $server, $fd, $data, $from_id)
2222
{
2323
return 'hello tcp';
2424
}
25-
}
26-
27-
$server = new DemoServer('tcp://0.0.0.0:9527');
2825

29-
$argv = $_SERVER['argv'];
26+
/**
27+
* Please return swoole configuration array.
28+
*
29+
* @return array
30+
*/
31+
public function configure()
32+
{
33+
// TODO: Implement configure() method.
34+
}
35+
}
3036

31-
$argv[1] = isset($argv[1]) ? $argv[1] : 'status';
37+
$server = new DemoServer('watch server', 'tcp://0.0.0.0:9527');
3238

33-
switch ($argv[1]) {
34-
case 'start':
35-
$server->start();
36-
break;
37-
case 'stop':
38-
$server->shutdown();
39-
break;
40-
case 'reload':
41-
$server->reload();
42-
break;
43-
case 'watch':
44-
$server->watch([__DIR__ . '/listen_files']);
45-
break;
46-
case 'status':
47-
default:
48-
$server->status();
49-
}
39+
$server->watch([__DIR__ . '/listen_files']);

src/Server.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ public function status()
437437
*/
438438
public function watch(array $directories = ['.'])
439439
{
440-
$self = $this;
440+
$that = $this;
441441

442442
if (false === ($status = check_process($this->name))) {
443-
$process = new swoole_process(function () use ($self) {
444-
$self->start();
443+
$process = new Process('server watch process', function () use ($that) {
444+
$that->start();
445445
}, true);
446446
$process->start();
447447
}
@@ -450,15 +450,15 @@ public function watch(array $directories = ['.'])
450450
$this->output->writeln(sprintf('Watching directory: ["<info>%s</info>"]', realpath($directory)));
451451
}
452452

453-
$watcher = new Watcher();
453+
$watcher = new Watcher($this->output);
454454

455-
$watcher->watch($directories, function () use ($self) {
456-
$self->reload();
455+
$watcher->watch($directories, function () use ($that) {
456+
$that->reload();
457457
});
458458

459459
$watcher->run();
460460

461-
swoole_process::wait();
461+
process_wait();
462462
}
463463

464464
/**

0 commit comments

Comments
 (0)