The current repo belongs to Paused status, and some functions are restricted. For details, please refer to the description of repo status
1 Star 0 Fork 2

黄毅/laravel-swoole-http
Paused

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
黄毅-huang-yi 黄毅 Fix typo a06b53d 7 years ago
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

Laravel-Swoole-Http

License Build Status

A high performance HTTP server based on Swoole. And now, it also supports WebSocket server.

Translations

Requirements

  • Laravel/Lumen 5.2+
  • ext-swoole 1.9.5+
  • ext-inotify (Optional)

Installation & Setup

  1. This package can be installed via composer:
$ composer require huang-yi/laravel-swoole-http
  1. Register service provider(Optional)

You need to register the service provider manually, if your Laravel version is less than 5.5 or you are using Lumen:

<?php
// Framework: Laravel
// File: config/app.php

[
    'providers' => [
        HuangYi\Swoole\SwooleServiceProvider::class,
    ],
];
<?php
// Framework: Lumen
// File: bootstrap/app.php

$app->register(HuangYi\Swoole\SwooleServiceProvider::class);
  1. Configurations

Run this command to publish the configuration file:

$ php artisan vendor:publish --provider="HuangYi\Swoole\SwooleServiceProvider"

Click here for more information about the configurations.

  1. Start server

Run this command to start the server:

php artisan swoole:server

And then, you can visit your website via http://127.0.0.1:1215.

Click here for more information about the commands.

Configurations

driver

This option controls the default "driver" that will be used to create the server.

Supported: "http", "websocket"

host

The ip address of the server.

port

The port of the server.

options

The swoole server configurations. Get more information from the official documents.

Notice: The value of task_worker_num must be greater than 0 if you use task.

resets

This option controls the instances that need to be reset after each request. It helps developers avoid problems caused by singleton, such as auth.

message_parser

The WebSocket message parser. You can replace this option with a custom parser.

Notice: The custom parser must implement the HuangYi\Swoole\Contracts\ParserContract.

redis

  • connection: Specify a redis connection to store websocket rooms and clients.
  • prefix: Set the prefix of redis key.

tables

Define swoole table structures:

<?php

[
    'tables' => [
        [
            'name' => 'users',
            'size' => 1024,
            'columns' => [
                ['id', 'int', 8],
                ['nickname', 'string', 255],
                ['score', 'float']
            ],
        ],
    ],
];

  • name: Specifies a table name.
  • size: Defines the maximum number of rows.
  • columns: Defines the table's columns. Format: [column_name, column_type, column_length]. Column types: int, integer, string, varchar, char, float.

Click here for more information about the tables.

watcher

The file watcher configurations.

directories:Defines the list of directories being watched;

excluded_directories:Defines a list of directories not being watched;

suffixes:Defines the list of file suffixes being watched.

Commands

This package provides an artisan command to manage the swoole server.

php artisan swoole:server

This command has an "action" argument and the default value is "start".

Actions Description
start Start the swoole server.
stop Stop the swoole server.
reload Reload the swoole server.
restart Restart the swoole server.
watch Enter the watch mode. The swoole server will reload automatically when the watched files changed.

Notice: The swoole server can only run in cli environment.

Tables

The Swoole Table can help developers to share data across worker processes. You can define the table structures in the configuration file.

<?php
use HuangYi\Swoole\Facades\Table;

// Insert a record.
Table::use('users')->set(1, ['id' => 1, 'nickname' => 'Bob', 'score' => 9.5]);

// Query a record.
$bob = Table::use('users')->get(1);
$nickname = Table::use('users')->get(1, 'nickname');

// Truncate a table.
Table::truncate('users');

Click here for more information about the Swoole Tables.

Task

Define a task:

<?php

use HuangYi\Swoole\Contracts\TaskContract;
use Illuminate\Support\Facades\Mail;

class SendMailTask implements TaskContract
{
    /**
     * @var array $mail
     */
    protected $mail;

    /**
     * Mail task
     * 
     * @var array $mail
     * @return void
     */
    public function __construct(array $mail)
    {
        $this->mail = $mail;
    }

    /**
     * Task handler.
     *
     * @param \Swoole\Server $server
     * @param int $taskId
     * @param int $srcWorkerId
     * @return void
     */
    public function handle($server, $taskId, $srcWorkerId)
    {
        Mail::to($this->mail['to'])->send($this->mail['view'], $this->mail['data']);
    }
}

Send the task to the task worker processes:

<?php

$task = new SendMailTask([
    'to' => 'bob@mail.com',
    'view' => 'mail',
    'data' => [],
]);

app('swoole.server')->task($task);

Notice: To start the task worker processes, the value of task_worker_num must be set to greater than 0.

Nginx

server {
    listen 80;
    server_name your.domain;
    root /path/to/laravel/public;
    index index.php;

    location = /index.php {
        # Ensure that there is no such file named "not_exists" in your "public" directory.
        try_files /not_exists @swoole;
    }

    location / {
        try_files $uri $uri/ @swoole;
    }

    location @swoole {
        set $suffix "";
        
        if ($uri = /index.php) {
            set $suffix "/";
        }

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://127.0.0.1:1215$suffix;
    }
}

Notice: Add the ip of swoole server (the default is 127.0.0.1) to the middleware App\Http\Middleware\TrustProxies.

Tips

  • Never use these functions: sleep()exit()die().
  • Be careful of the singletons.
MIT License Copyright (c) 2018 Huang Yi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

基于Swoole的高性能HTTP Server,加速你的Laravel和Lumen应用。 expand collapse
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/huang-yi/laravel-swoole-http.git
git@gitee.com:huang-yi/laravel-swoole-http.git
huang-yi
laravel-swoole-http
laravel-swoole-http
master

Search