Home > PHP Framework > Swoole > body text

How to use swoole in laravel

爱喝马黛茶的安东尼
Release: 2019-12-12 10:28:13
Original
6469 people have browsed it

How to use swoole in laravel

PHP's asynchronous, parallel, high-performance network communication engine is written in pure C language and provides an asynchronous multi-threaded server in PHP language, asynchronous TCP/UDP network client, and asynchronous MySQL , asynchronous Redis, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file reading and writing, asynchronous DNS query. Swoole has built-in Http/WebSocket server/client and Http2.0 server.

The documentation on Swoole's official website is not rich enough, which is a headache, but most of the problems are explained. If you are interested in Swoole, then check out this Swoole introductory tutorial. Swoole provides many awesome functions such as multi-threading and long connections, which brings PHP to a new level. For details, you can read the introductory tutorial. This article is limited to discussing the combination of Laravel and Swoole.

In order to provide services, Swoole must run in CLI mode. What is CLI mode? If your Swoole business code is written in a file called server.php, then enter php server.php on the command line to open it. This is a headache, because the Laravel framework does not work like this, so how can it be combined with Laravel? That’s right, it’s that simple to customize an Artisan Command.

STEP 1-Customize Command

About customizing Artisan Commnad, all the technical points you need to know are here. I customized a command called SwooleCommand. Directly paste the key code:

How to use swoole in laravel

fire is the entrance

Execute php artisan swoole start on the command line (CLI) to start the Swoole service. Analyzing the code, you can see that the command parameters include startup, restart, and shutdown. I only implemented the startup part to save trouble. If you need to shut down, use the kill command to shut down the process in Linux. The steps are quite simple:

1. Execute the ps -aux|grep artisan command to get the pid (there are multiple processes, just kill the first one)

2. Execute the kill pid command, the pid is the one you obtained in the first step

The configuration of Swoole is not within the scope of this article. Please go to the official website. The Swoole service is saved in the $serv variable here for Laravel to send commands for interaction later. You can see that Swoole's event response code is like this:

How to use swoole in laravel

Use Handler to handle event responses

If fire opens the door to Swoole, then The handler here is the conveyor belt between Swoole and Laravel. Using the handler you write, you can write various business logic into the Laravel framework, and then you can use the various efficient and convenient functions provided by Laravel. "Handler" is a naming convention. You can also call it "callback", "manager", or "listener". It depends on your naming habits. I did not use the new method but used Laravel's IoC to inject App::make, mainly to save trouble (because the handler's constructor uses my custom data processing class, see below).

STEP 2-Custom handler

Because it is a custom class, please follow the namespace and declare it in composer.json. After completion, execute composer dump-autoload Update the command. For example, if I create a folder app\handlers to store handlers, it looks like this in composer.json:

How to use swoole in laravel

autoload cannot be less

Then It's up to you to decide what exactly is done in the handler. Anyway, it’s almost the same as writing a controller. You can use all the functions of the Laravel framework at will. Paste mine:

How to use swoole in laravel

I mentioned in the previous section that I use IoC because of the structure The server uses its own data processing class. I put additions, deletions, modifications, queries, and other data processing services into the Repository. There is no other reason, but this way the code looks cleaner. In this way, the process of using Swoole to receive data is completed, but what should I do if I want to use Swoole to send data to the client? Ahem, this is a little more troublesome and requires a curve method to implement. Continue to the next section.

STEP 3-Send data

There are two methods, but both are inseparable from a cache kv structure (Laravel’s own Cache function is enough), save The client's address data, otherwise how would you know where to send it. I use the first one, which saves trouble. Swoole has nothing to do with sending data. If you need a long-term websocket connection, this does not apply. Just use the second one. If you have a better way, please tell me!

The first one: fsockopen

It’s quite simple and has nothing to do with swoole. Use Swoole’s connection_info function to get the client’s IP address and port, and then use fsockopen to send data directly.

Second type: Internal port listening

Swoole supports monitoring multiple ports. The idea of ​​implementation is to use fsockopen to send data to the internal listening port, and then call $serv to send messages. The advantage of this is that you don't need to know the actual IP address and port of the client. You can save the client's $fd identifier in the Cache and send the data directly. Using this idea, please remember to open the port with iptables. I didn't use it myself, because I thought it was too troublesome without a long connection.

Summary

Swoole is very good, but I haven’t actually used it much (I’ll wait until I have enough money for the project). You can also refer to the configuration on the official website and use Swoole as the nginx hosting agent. It is said that the performance is greatly improved.

PHP Chinese website has a large number of free Swoole introductory tutorials, everyone is welcome to learn!

The above is the detailed content of How to use swoole in laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!