Download the PHP package cyzonetech/think-swoole without Composer

On this page you can find all versions of the php package cyzonetech/think-swoole. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package think-swoole

ThinkPHP 5.1 Swoole 扩展

安装

首先按照Swoole官网说明安装swoole扩展,然后使用

composer require topthink/think-swoole

安装swoole扩展。

使用方法

HttpServer

直接在命令行下启动服务端。

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]

Server

可以支持直接启动一个Swoole server

php think swoole:server

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

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

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

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

return [
    // 扩展自身配置
    'host'         => '0.0.0.0', // 监听地址
    'port'         => 9501, // 监听端口
    'type'         => 'socket', // 服务类型 支持 socket http server
    'mode'         => SWOOLE_PROCESS,
    'sock_type'    => SWOOLE_SOCK_TCP,

    // 可以支持swoole的所有配置参数
    'daemonize'    => false,

    // 事件回调定义
    'onOpen'       => function ($server, $request) {
        echo "server: handshake success with fd{$request->fd}\n";
    },

    'onMessage'    => function ($server, $frame) {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    },

    'onRequest'    => function ($request, $response) {
        $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
    },

    'onClose'      => function ($ser, $fd) {
        echo "client {$fd} closed\n";
    },
];

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

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

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

return [
    'swoole_class'  =>  'app\http\Swoole',
];

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

在命令行启动服务端

php think swoole:server

支持reload|restart|stop|status 操作

php think swoole:server reload

配置信息详解

swoole.php

timer.php

异步任务投递

1.异步任务接口实现

2.异步任务投递在控制器中的使用

定时器的使用

定时器分为系统定时器和自定义定时器,系统定时器需要在配置文件(timer.php)中进行配置,会根据配置由系统自动处理

  1. 定时器接口的实现

  2. 系统定时器的使用

参考上面timer.php的配置方法,系统定时器任务会自动进行异步投递,因此必须在swoole.php中配置task_worker_num,系统会自动调用非繁忙的task worker进行任务处理

  1. 自定义定时器

自定义定时器可以执行闭包和Timer接口实现类。注意,如非必要请勿在控制器等重复调用的地方使用tick方法,因为每次请求都会创建新的定时器。如果必须创建,请注意定时器回收。

获取Server

获取Swoole实例对象

自定义服务启动

如果需要在Swoole启动的时候创建一些服务,可以按照如下方法进行自定义

在swoole.php中配置配置

//闭包方式

如果需要实现Workerstart的接口,可以这样实现

配置文件

Websocket支持

需要在swoole.php修改

由于Swoole\WebSocket\Server继承于Swoole\Http\Server顾Http服务具有的功能Websocket也具有。如果采用如下websocket服务, 通讯数据格式已经固定,如采用自定义数据结构,请采用自定义服务

WebSocket通讯数据结构

详解

客户端JS可以参考example里websocketclient.js

服务端发送数据

新增队列支持

think-queue,是一个非常好用的队列服务,xavier-swoole的队列服务依赖于think-queue,使用方法和think-queue保持一致

主要增加如下功能

  1. 采用多进程模式运行或采用Task异步执行
  2. 可以指定同时消耗队列任务的进程数量
  3. Task模式和Server共享Task进程,充分利用资源
  4. Process模式采用进程模式,各个任务之间相互隔离,执行一定次数后,重启进程,防止内存泄露

如果任务较多且复杂,推荐采用Process模式


All versions of think-swoole with dependencies

PHP Build Version
Package Version
Requires php Version >7.1
ext-json Version *
ext-swoole Version >=4.4.8
nette/php-generator Version ^3.2
open-smf/connection-pool Version ~1.0
swoole/ide-helper Version ^4.3
symfony/finder Version ^4.3.2|^5.1
cyzonetech/framework Version ^6.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package cyzonetech/think-swoole contains the following files

Loading the files please wait ....