2017 © Pedro Peláez
 

library swoole

FastD swoole.

image

fastd/swoole

FastD swoole.

  • Friday, July 6, 2018
  • by JanHuang
  • Repository
  • 10 Watchers
  • 71 Stars
  • 5,317 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 15 Forks
  • 1 Open issues
  • 35 Versions
  • 12 % Grown

The README.md

FastD Swoole

Build Status PHP Require Version Swoole Require Version Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

高性能网络服务组件. 提供底层服务封装, 基础管理及客户端调用功能. 使用 composer 进行管理, 可在此基础上进行封装整合., (*2)

环境要求

  • Linux (不考虑windows)
  • PHP >= 5.6
  • swoole >= 1.9.6

源码地址: swoole, (*3)

pecl 安装, (*4)

pecl install swoole

可选扩展

如果 PHP >= 7.0 的安装 2.0 版本., (*5)

源码地址: inotify, (*6)

pecl 安装, (*7)

pecl install inotify

安装

composer require fastd/swoole

文档

中文文档, (*8)

使用

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

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

Swoole 配置通过实现 configure 方法进行配置,具体配置参数请参考: Swoole 配置选项, (*11)

TCP Server

class DemoServer extends \FastD\Swoole\Server\Tcp
{
    public function doWork(swoole_server $server, $fd, $data, $from_id)
    {
        echo $data . PHP_EOL;
        return 'hello tcp';
    }
}

DemoServer::createServer('tcp swoole', 'tcp://0.0.0.0:9527')->start();

UDP Server

class DemoServer extends \FastD\Swoole\Server\Udp
{
    public function doPacket(swoole_server $server, $data, $client_info)
    {
        echo $data . PHP_EOL;
        return 'hello tcp';
    }
}

DemoServer::createServer('udp swoole', 'udp://127.0.0.1:9527')->start;

HTTP Server

同理, Http 服务器扩展 Server 类, 实现 doRequest 方法,实现具体逻辑。, (*12)

class Http extends \FastD\Swoole\Server\Http
{
    public function doRequest(ServerRequest $serverRequest)
    {
        return new JsonResponse([
            'msg' => 'hello world',
        ]);
    }
}

Http::createServer('http', 'http://0.0.0.0:9527')->start();

目前 Http 服务支持 Session 存储,而 Session 存储是基于浏览器 cookie,或者可以自定义实现存储方式。, (*13)

目前由 FastD/Session 提供 session 支持以及 swoole_http_request 对象解释。, (*14)

WebSocket Server

class WebSocket extends \FastD\Swoole\Server\WebSocket
{
    public function doOpen(swoole_websocket_server $server, swoole_http_request $request)
    {
        echo "server: handshake success with fd{$request->fd}\n";
    }

    public function doMessage(swoole_server $server, swoole_websocket_frame $frame)
    {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    }
}

WebSocket::createServer('ws', 'ws://0.0.0.0:9527')->start();

多端口支持

class Server extends \FastD\Swoole\Server\Tcp
{
    public function doWork(swoole_server $server, $fd, $data, $from_id)
    {
        return 'hello server1';
    }
}

class Server2 extends \FastD\Swoole\Server\Tcp
{
    public function doWork(swoole_server $server, $fd, $data, $from_id)
    {
        return 'hello server2';
    }
}

$server = new Server('tcp server', 'tcp://127.0.0.1:9527');

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

$server->start();

服务管理

class DemoServer extends \FastD\Swoole\Server\Tcp
{
    public function doWork(swoole_server $server, $fd, $data, $from_id)
    {
        echo $data . PHP_EOL;
        return 'hello tcp';
    }
}

$server = DemoServer::createServer('tcp swoole', 'tcp://0.0.0.0:9527');

$argv = $_SERVER['argv'];

$argv[1] = isset($argv[1]) ? $argv[1] : 'status';

switch ($argv[1]) {
    case 'start':
        $server->start();
        break;
    case 'stop':
        $server->shutdown();
        break;
    case 'reload':
        $server->reload();
        break;
    case 'status':
    default:
        $server->status();
}

File Listener

因为 swoole 是常驻内存,程序通过首次启动就自动预载到内存当中,所以每次修改都需要重启服务。, (*15)

所以这里提供监听文件变化来到自动重启服务(建议开发环境中使用), (*16)


class DemoServer extends \FastD\Swoole\Server\Tcp { public function doWork(swoole_server $server, $fd, $data, $from_id) { return 'hello tcp'; } } $server = new DemoServer('watch server', 'tcp://0.0.0.0:9527'); // $server = DemoServer::createServer('watch server', 'tcp://0.0.0.0:9527'); $server->watch([__DIR__ . '/listen_files']);

Sync Client

Client 通过 resolve 执行,通过不同的方法设置不同的回调,同步、异步均使用通用的方法。, (*17)

$client = new \FastD\Swoole\Client\Sync\SyncClient('tcp://11.11.11.11:9527');

$client
    ->connect(function ($client) {
        $client->send('hello world');
    })
    ->receive(function ($client, $data) {
        echo $data . PHP_EOL;
        $client->close();
    })
    ->resolve()
;

Async Client

$client = new \FastD\Swoole\Client\Async\AsyncClient('tcp://11.11.11.11:9527');

$client
    ->connect(function ($client) {
        $client->send('hello world');
    })
    ->receive(function ($client, $data) {
        echo $data . PHP_EOL;
    })
    ->error(function ($client) {
        print_r($client);
    })
    ->close(function ($client) {})
    ->resolve()
;

Process

$process = new Process('single', function () {
    timer_tick(1000, function ($id) {
        static $index = 0;
        $index++;
        echo $index . PHP_EOL;
        if ($index === 10) {
            timer_clear($id);
        }
    });
});

$process->start();

$process->wait(function ($ret) {
    echo 'PID: ' . $ret['pid'];
});

Multi Process

$process = new Process('multi', function () {
    timer_tick(1000, function ($id) {
        static $index = 0;
        $index++;
        echo $index . PHP_EOL;
        if ($index === 10) {
            timer_clear($id);
        }
    });
});

$process->fork(5);

$process->wait(function ($ret) {
    echo 'PID: ' . $ret['pid'] . PHP_EOL;
});

Queue

$queue = new \FastD\Swoole\Queue('queue', function ($worker) {
    while (true) {
        $recv = $worker->pop();
        echo "From Master: $recv\n";
    }
});

$queue->start();

while (true) {
    $queue->push('hello');
    sleep(1);
}


$queue->wait(function ($ret) {
    echo 'PID: ' . $ret['pid'];
});

贡献

非常欢迎感兴趣,愿意参与其中,共同打造更好PHP生态,Swoole生态的开发者。, (*18)

如果你乐于此,却又不知如何开始,可以试试下面这些事情:, (*19)

联系

如果你在使用中遇到问题,请联系: bboyjanhuang@gmail.com. 微博: 编码侠, (*20)

License MIT

The Versions

06/07 2018

dev-master

9999999-dev

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

06/07 2018

v2.3.0

2.3.0.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

08/05 2018

v2.2.2

2.2.2.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

08/05 2018

v2.2.1

2.2.1.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

24/01 2018

v2.2.0

2.2.0.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

24/01 2018

dev-develop

dev-develop

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

30/11 2017

v2.2.0-beta4

2.2.0.0-beta4

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

30/11 2017

v2.2.0-beta3

2.2.0.0-beta3

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

29/11 2017

v2.2.0-beta2

2.2.0.0-beta2

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

25/11 2017

v2.2.0-beta1

2.2.0.0-beta1

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

24/11 2017

2.0.x-dev

2.0.9999999.9999999-dev

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

24/11 2017

v2.1.0

2.1.0.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

13/10 2017

v2.0.3

2.0.3.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

20/09 2017

v2.0.2

2.0.2.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

24/08 2017

v2.0.1

2.0.1.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

04/07 2017

5.0.x-dev

5.0.9999999.9999999-dev

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

01/05 2017

v2.0.0

2.0.0.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

27/04 2017

v2.0.0-rc1

2.0.0.0-RC1

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

27/04 2017

v2.0.0-beta3

2.0.0.0-beta3

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

01/04 2017

v2.0.0-beta2

2.0.0.0-beta2

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

17/03 2017

v2.0.0-beta1

2.0.0.0-beta1

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

26/02 2017

1.1.x-dev

1.1.9999999.9999999-dev

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

24/02 2017

1.0.x-dev

1.0.9999999.9999999-dev

FastD swoole.

  Sources   Download

MIT

The Requires

 

by Avatar JanHuang

24/02 2017

v1.0.5

1.0.5.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

by Avatar JanHuang

17/02 2017

v1.0.4

1.0.4.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

17/02 2017

v1.0.3

1.0.3.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

14/02 2017

v1.0.2

1.0.2.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

07/02 2017

v1.0.1

1.0.1.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

02/02 2017

v1.0.0-beta3

1.0.0.0-beta3

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

02/02 2017

v1.0.0-rc1

1.0.0.0-RC1

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

02/02 2017

v1.0.0

1.0.0.0

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

17/01 2017

v1.0.0-beta2

1.0.0.0-beta2

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

14/01 2017

v1.0.0-alpha2

1.0.0.0-alpha2

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

14/01 2017

v1.0.0-beta1

1.0.0.0-beta1

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

01/01 2017

v1.0.0-alpha1

1.0.0.0-alpha1

FastD swoole.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang