What to do if swoole json is incomplete
What should I do if the swoole json is incomplete?
swooleSolution to the integrity problem of sending and receiving data between the client and the server
1. In the following example, after starting the swoole service, it listens to the 9501 port and receives data from The data sent by the client is returned unchanged.
class Server { private $serv; public function __construct() { $this->serv = new swoole_server("127.0.0.1", 9501); $this->serv->set(array( 'worker_num' => 4, //一般设置为服务器CPU数的1-4倍 'daemonize' => 1, //以守护进程执行 'max_request' => 2000, 'dispatch_mode' => 2,//进程数据包分配模式 1平均分配,2按FD取摸固定分配,3抢占式分配 'task_worker_num' => 8, //task进程的数量 "task_ipc_mode " => 3 , //使用消息队列通信,并设置为争抢模式 "log_file" => "./log/taskqueueu.log" ,//日志 )); $this->serv->on('Receive', array($this,'onReceive'));//接收到数据时回调此函数 $this->serv->start(); } public function onReceive(swoole_server $serv, $fd, $from_id, $data ) { $serv->send($fd, $data); usleep(500); //不加延时的话,经常两条数据被合并成一条返回了。 $serv->close($fd); } public function onClose(swoole_server $serv, $fd) { $serv->send($fd, 'CLOSED'); } }
Start the server directly with new Server.
2. Start the swoole client, send data to the server, and receive the return.
$client = new swoole_client(SWOOLE_SOCK_TCP); if (!$client->connect('127.0.0.1', 9501, -1)) { exit("connect failed. Error: {$client->errCode}\n"); } $data=[ 'type'=>1, 'data'=>array( 'PlatformCode'=>'...........很长的数据.', ) ]; $sender=$client->send(json_encode($data)."\r\n\r\n"); while($result = $client->recv()){ if($result=='CLOSED'){ echo "任务结束。byebye~\r\n"; break; }else{ echo $result; } } $client->close();
When sending a relatively large data packet, you will find that the received json packet is incomplete, which can be solved by using the EOF protocol processing method, such as setting on the server:
$serv->set( array('open_eof_split' => TRUE, 'package_eof' => "\r\n\r\n") );
In this way, '\r\n\r\n' becomes the EOF protocol end character.
When sending a data packet, add '\r\n\r\n' at the end of the packet. When this character is encountered when the interface data is encountered, it is considered that the data has been received, thus ensuring the integrity of the data.
Note: The '\r\n\r\n' character cannot appear in the data packet, otherwise a subpackaging error will occur.
Swoole's Server and asynchronous Client both process data packets in the onReceive callback function. When protocol processing is set, the onReceive event will only be triggered when a complete data packet is received.
Another way, you can also pre-set the length of the packet to be sent, such as:
$server->set(array( 'open_length_check' => true, 'package_max_length' => 81920, 'package_length_type' => 'n', //see php pack() 'package_length_offset' => 0, 'package_body_offset' => 2, ));
The length of the packet can be fixed to ensure the integrity of the data. The official comment is as follows:
The protocol for fixed headers is very common and can often be seen in BAT server programs. The characteristic of this protocol is that a data packet always consists of a packet header and a packet body. The packet header has a field that specifies the length of the packet body or the entire packet. The length is generally represented by a 2-byte/4-byte integer. After the server receives the packet header, it can accurately control how much more data it needs to receive based on the length value to achieve a complete data packet. Swoole's configuration can support this protocol very well and can flexibly set four parameters to deal with all situations.
Swoole's Server and asynchronous Client both process data packets in the onReceive callback function. When protocol processing is set, the onReceive event will only be triggered when a complete data packet is received. After the synchronization client sets up protocol processing, calling $client->recv() no longer requires passing in the length. The recv function returns after receiving the complete packet or an error occurs.
The above is the detailed content of What to do if swoole json is incomplete. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Swoole coroutines in Laravel can process a large number of requests concurrently. The advantages include: Concurrent processing: allows multiple requests to be processed at the same time. High performance: Based on the Linux epoll event mechanism, it processes requests efficiently. Low resource consumption: requires fewer server resources. Easy to integrate: Seamless integration with Laravel framework, simple to use.

How to use Swoole to implement a high-performance HTTP reverse proxy server Swoole is a high-performance, asynchronous, and concurrent network communication framework based on the PHP language. It provides a series of network functions and can be used to implement HTTP servers, WebSocket servers, etc. In this article, we will introduce how to use Swoole to implement a high-performance HTTP reverse proxy server and provide specific code examples. Environment configuration First, we need to install the Swoole extension on the server

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.

Performance comparison: Throughput: Swoole has higher throughput thanks to its coroutine mechanism. Latency: Swoole's coroutine context switching has lower overhead and smaller latency. Memory consumption: Swoole's coroutines occupy less memory. Ease of use: Swoole provides an easier-to-use concurrent programming API.

To restart the Swoole service, follow these steps: Check the service status and get the PID. Use "kill -15 PID" to stop the service. Restart the service using the same command that was used to start the service.

Swoole Process allows users to switch. The specific steps are: create a process; set the process user; start the process.

Swoole in action: How to use coroutines for concurrent task processing Introduction In daily development, we often encounter situations where we need to handle multiple tasks at the same time. The traditional processing method is to use multi-threads or multi-processes to achieve concurrent processing, but this method has certain problems in performance and resource consumption. As a scripting language, PHP usually cannot directly use multi-threading or multi-process methods to handle tasks. However, with the help of the Swoole coroutine library, we can use coroutines to achieve high-performance concurrent task processing. This article will introduce

Swoole is a high-performance PHP network development framework. With its powerful asynchronous mechanism and event-driven features, it can quickly build high-concurrency and high-throughput server applications. However, as the business continues to expand and the amount of concurrency increases, the CPU utilization of the server may become a bottleneck, affecting the performance and stability of the server. Therefore, in this article, we will introduce how to optimize the CPU utilization of the server while improving the performance and stability of the Swoole server, and provide specific optimization code examples. one,
