
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);
$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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment





