Rumah  >  Artikel  >  pembangunan bahagian belakang  >  直击Thinkphp中的Swoole

直击Thinkphp中的Swoole

coldplay.xixi
coldplay.xixike hadapan
2020-06-08 10:54:394409semak imbas


  

直击Thinkphp中的Swoole

Thinkphp中使用Swoole

Swoole是一个面向生产环境的 PHP 异步网络通信引擎。使 PHP 开发人员可以编写高性能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。

安装

首先按照Swoole官网说明安装swoole扩展,然后安装think-swoole扩展。

composer require topthink/think-swoole=2.0.*使用

使用

Swoole目前不支持Windows

使用Swoole作为HttpServer

命令行下启动服务端(需要2.0+版本think-swoole扩展)
直接在命令行下启动服务端。

php think swoole

启动完成后,会在0.0.0.0:9501启动一个HTTP Server,可以直接访问当前的应用。

swoole的参数可以在应用配置目录下的swoole.php里面配置(具体参考配置文件内容)。

如果需要使用守护进程方式运行,可以使用

php think swoole -d

或者在swoole.php文件中设置

'daemonize' =>   true

注意:由于onWorkerStart运行的时候没有HTTP_HOST,因此最好在应用配置文件中设置app_host

支持的操作包括

php think swoole [start|stop|reload|restart]

由于onWorkerStart运行的时候没有HTTP_HOST,因此最好在应用配置文件中设置app_host参数

启动后,可以使用

http://127.0.0.1:9501

访问你的应用。

如果需要配置地址和端口,可以在应用配置目录下增加
swoole.php配置文件,然后设置:

<?phpreturn [
    &#39;host&#39;  => &#39;tp5.com&#39;,
    &#39;port&#39;  =>   9508,];
可以支持Swoole自身的配置参数设置,例如:
<?phpreturn [
    &#39;host&#39;          => &#39;tp5.com&#39;,
    &#39;port&#39;          =>   9508,
    &#39;worker_num&#39;    =>   4,
    &#39;max_request&#39;   =>   1000,];

扩展中定义了onWorkerStart和onRequest事件回调方法(如果不熟悉请不要随意替换),如果你需要自定义swoole的事件回调方法,可以在配置文件中使用闭包定义。

<?phpreturn [
    &#39;host&#39;          => &#39;tp5.com&#39;,
    &#39;port&#39;          =>   9508,
    &#39;worker_num&#39;    =>   4,
    &#39;max_request&#39;   =>   1000,
    &#39;WorkerStop&#39;    =>   function($server, $worker_id){
        // 添加你的代码
    },];

或者直接在配置文件中添加

使用Swoole作为Server服务端

可以支持直接启动一个Swoole server(需要2.0.9+版本)

php think swoole:server

会在0.0.0.0:9508启动一个Websocket服务。

如果需要自定义参数,可以在config/swoole_server.php中进行配置,包括:

配置参数
描述
type 服务类型
host 监听地址
port 监听端口
mode 运行模式
socket Socket type

并且支持swoole所有的参数。
也支持使用闭包方式定义相关事件回调。

return [
    // 扩展自身配置
    &#39;host&#39;         => &#39;0.0.0.0&#39;, // 监听地址
    &#39;port&#39;         => 9501, // 监听端口
    &#39;type&#39;         => &#39;socket&#39;, // 服务类型 支持 socket http server
    &#39;mode&#39;         => SWOOLE_PROCESS,
    &#39;socket_type&#39;  => SWOOLE_SOCK_TCP,
 
    // 可以支持swoole的所有配置参数
    &#39;daemonize&#39;    => false,
 
    // 事件回调定义
    &#39;onOpen&#39;       => function ($server, $request) {
        echo "server: handshake success with fd{$request->fd}\n";
    },
 
    &#39;onMessage&#39;    => function ($server, $frame) {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    },
 
    &#39;onRequest&#39;    => function ($request, $response) {
        $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
    },
 
    &#39;onClose&#39;      => function ($ser, $fd) {
        echo "client {$fd} closed\n";
    },];

也可以使用自定义的服务类

<?php
namespace app\http;use think\swoole\Server;class Swoole extends Server{
    protected $host = &#39;127.0.0.1&#39;;
    protected $port = 9502;
    protected $option = [ 
        &#39;worker_num&#39;=> 4,
        &#39;daemonize&#39; => true,
        &#39;backlog&#39;   => 128
    ];
 
    public function onReceive($server, $fd, $from_id, $data)
    {
        $server->send($fd, &#39;Swoole: &#39;.$data);
    }}

支持swoole所有的回调方法定义(回调方法必须是public类型)
serverType 属性定义为 socket或者http 则支持swoole的swoole_websocket_server和swoole_http_server

然后在swoole_server.php中增加配置参数:

return [
    &#39;swoole_class&#39;  =>   &#39;app\http\Swoole&#39;,];

定义该参数后,其它配置参数均不再有效。

在命令行启动服务端

php think swoole:server

支持reload|restart|stop|status 操作

php think swoole:server reload

推荐教程:《PHP视频教程

Atas ialah kandungan terperinci 直击Thinkphp中的Swoole. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:liqingbo.cn. Jika ada pelanggaran, sila hubungi admin@php.cn Padam