Home > PHP Framework > Swoole > body text

What to do if swoole json is incomplete

藏色散人
Release: 2020-04-09 10:16:49
Original
2479 people have browsed it

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');
    }   
}
Copy after login

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();
Copy after login

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")
    );
Copy after login

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,
));
Copy after login

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!

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!