Home > Article > Backend Development > A brief analysis of how PHP implements the commonly used instant delivery function in App_php example
Foreword
Imagine that when we usually post to WeChat Moments, N pictures are paired with text, and they are sent out in a flash without any sloppiness. The experience feels so cool~.
But let’s stop and think about it technically, is this possible? Some 2G networks only have a speed of tens of kilobytes at most. Our pictures are only a few MB in size, and even if they are compressed, they are hundreds of kilobytes in size. How is it possible to send a message instantly?
Now that I think about it, isn’t it a bit weird~
Actually, many social software (Weibo, WeChat) use a kind of instant messaging mechanism. It doesn’t actually send it first and then tell you that it was sent successfully. Instead, it directly tells you that it was sent successfully, and then secretly uploads what you sent in the background. Therefore, we often find a phenomenon when the network speed is not good. , the newly sent Moments were normal at first, but after a few minutes, we were prompted that the sending failed! This is very embarrassing. You didn’t say anything at the beginning, but at the most critical moment you told me that you couldn’t do it..
Don’t be intimidated by the advanced technology. It’s just some small tricks. It’s really basic, but it’s also really practical.
First state some initial conditions before discussing the technology.
1. Some special modifications have been made to the database table structure: The content table of the circle of friends has a special field status. There are two status values. The value can be 1 or 2,
A value of 1 means that the circle of friends has not been published. A value of 2 means that the circle of friends has been published. (Students who don’t understand why this is done for now can continue reading first and I will explain it later)
2. The second-post function of this article refers to the use when there are pictures. Because if there are pictures, the image upload is too slow, so the second-post mechanism needs to be used. However, if there is no picture and only text, it is not necessary. , because the text transmission volume is very low, just send it according to the normal process.
3. The code of this article is based on the PhalApi framework, and the syntax is relatively simple. Students who have experience in ORM operations should understand it
4. This article mainly explains the second sending function of the APP. This function is not particularly needed on the WEB side, because the modern network is enough for our PC to send many, many pictures at once (10M/s, 20M/s)
Discuss the entire execution process in general:
The client calls the publishing API, and the server publishes the content (publish.php). If there are pictures, the client also calls an additional upload API (upload.php). In this upload API (upload .php) before the work is completed, the client will directly tell you that the release is successful (in fact, the upload is not completed currently, there is a process behind it that is desperately helping you upload), and then the client will send the text and pictures you sent. Temporarily splice it and display it for you (currently only you can see it, others in your circle of friends cannot see it), and then wait for the result of the upload API (upload.php)/of course the upload may time out (usually one The result will be available within minutes). If it succeeds, it will be uploaded smoothly. If it fails, it will report a failure to send. However, within one minute of waiting for the result, it will first let you think that you have sent it. Unless the upload fails, it will be reported later. remind you.
So let’s analyze this mechanism at a technical level:
When we click the send button in the upper right corner, two processes are started at the same time. One of them is to help you upload the text and tell you that it has been sent successfully (publish.php), and the other process is to secretly upload it. The picture you sent (upload.php), the specific code is as follows:
Publish.php
<?php //正常获取数据(文本,图片,位置信息等) … Code … Code … //进行判断,如果有图片则为未发布(status为1),无图片则为立即发布(status为2) //如果有图片则通过返回标识符告诉客户端,让他赶紧去调用真实的上传逻辑upload.php,我们这只把最基本的文本上传好,再设置多一个status而已~ $status = ($pic_num > 0) ? 1 : 2; //拼接入库数据 $where_data = array( "status"=> $status) //数据入库 DI()->notorm-> friends ->insert($where_data); ?>
Did you see the mystery? We made a judgment on the status field entered into the database. There are two situations: 1 (unpublished) and 2 (published). So what should we do when reading the data (list.php )?
Then the display page is like this:
Lists.php
<?php //code .. //获取文字信息 Code.. //获取图片信息 //(它在获取当前用户pic表内的f_id(即获取朋友圈图片),最关键的地方是where条件 $data= DI()->notorm->pic->select('f_id')->where("status > 1 OR (status = 1 && u_id = {$u_id})")->->fetchAll(); //code .. ?>
此处的where条件是秒发机制的最关键的地方:
status大于1(已发表)或者等于1(未发布),(tips:status在有图片的情况下默认值为1)但是属于当前用户发布的内容,都可以读出来,这就有一个很奇妙的现象,就是无论如何,我们自己发的朋友圈,自己永远是可以读出来的,但是其他人就不一定了(因为如果有图片的话,还需要去调用另外一个进程上传图片,然后在那个进程将status改为2)
那么还有最后一个关键点,就是负责上传图片的那个进程(upload.php),这个是真实上传图片的逻辑,
有几张图片,这么upload.php就会被调用几次
每次上传成功后将图片行的字段status改成2
upload.php
<?php //Code.. //把图片上传到服务器目录 //获取长传结果标识,更改状态 If(上传成功){ //将status改回2 $status_data = array("status" => 2); DI()->notorm->pic->select('u_id')->where('u_id, $u_id)-->update($status_data); }else{ Code… }
经过以上的几个操作(首先是publish.php,如果有图片上传的话则调用upload.php,展示的时候是list.php)。
不知道大家看出门道没有,和我们平常写的发布功能不同的是,上传upload.php功能被独立出来了,改装后的发布publish.php会用最快的速度将你的文本内容存进数据库,并且如果有图片内容的话,他会单独调用上传API upload.php。
最关键的是在显示的时候做了一些小技巧,让自己保证可以看到自己发的东西。
好了,以上就是本文的全部内容了,希望对大家学习PHP有所帮助,也请大家继续关注脚本之家。