Home > PHP Framework > Swoole > body text

swoole's onreceive is not triggered

(*-*)浩
Release: 2019-12-14 14:13:01
Original
2473 people have browsed it

swoole's onreceive is not triggered

onReceive

This function is called back when data is received, which occurs in the worker process. Function prototype: (Recommended learning: swoole video tutorial)

function onReceive(swoole_server $server, int $fd, int $reactor_id, string $data);
Copy after login

$server, Server object

$fd, the unique identifier of the TCP client connection

$reactor_id, the Reactor thread ID where the TCP connection is located

$data, the received data content may be text or binary content

About $fd and $ Detailed explanation of reactor_id

If the automatic protocol option is not turned on, the maximum data received by onReceive at a time is 64K

If the automatic protocol processing option is turned on, onReceive will receive the complete data packet , the maximum does not exceed package_max_length

Supports binary format, $data may be binary data

Use the open_eof_check/open_length_check/open_http_protocol provided by the bottom layer to ensure the integrity of the data package

Do not use the underlying protocol processing, analyze the data by yourself in the PHP code after onReceive, and merge/split the data packets.

For example: You can add a $buffer = array() to the code and use $fd as the key to save context data. Each time data is received, string splicing is performed, $buffer[$fd] .= $data, and then it is judged whether the $buffer[$fd] string is a complete data packet.

By default, the same fd will be assigned to the same Worker, so the data can be spliced ​​together. When using dispatch_mode = 3.

Requesting data is preemptive, and data sent from the same fd may be divided into different processes. Therefore, the above data packet splicing method cannot be used

Regarding the problem of sticky packets such as SMTP protocol, the client may issue 2 instructions at the same time. It may be received in the server all at once, in which case the application layer needs to unpack it by itself. SMTP is subpackaged through \r\n, so explode("\r\n", $data) is needed in the business code to split the data packets.

If it is a request-response service, there is no need to consider splitting the data. The reason is that after the client initiates a request, it must wait until the server returns the response data of the current request before initiating a second request. It will not send two requests at the same time

The above is the detailed content of swoole's onreceive is not triggered. 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!