Home > Article > Backend Development > How to implement the instant delivery function of APP in php
How does php implement the instant delivery function of APP? Many social software use a second-second mechanism to make users feel good about their physical examination. Today we will also talk about some tips commonly used in these social software. I hope to be helpful.
Preface
Imagine that when we usually post to WeChat Moments, we add N pictures with text, and they are sent out in a flash without any sloppiness. That kind of 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 megabytes each, even if they are compressed, they are hundreds of kilobytes. How is it possible to send a message instantly?
Now think about it, isn’t it a bit weird~
In fact, 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 it couldn't be done...
Don't be intimidated by the advanced technology, it's just done. Just some tips, really basic, but 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 circle of friends content table has a special field status. There are two status values. The value can be 1 or 2.
The value is 1. The circle of friends is not published. A value of 2 means that the circle of friends has been published. (Students who don’t understand why this is done at the moment can continue reading first, and it will be explained later)
2. The instant sending function of this article refers to using it when there are pictures, because there are pictures In this case, the image upload is too slow, so the second sending mechanism needs to be used. However, if there is no pure text in the image, it is not necessary, because the text transmission volume is very low, and it can be sent 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 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)
Let’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 , then the client will have to call an additional upload API (upload.php). When this upload API (upload.php) has not completed the work, the client will directly tell You publish successfully (in fact, the upload is not completed yet, there is a process behind it that is trying its best to help you upload), and then the client will temporarily splice the text and pictures you sent and display them to you (currently only you can see them, you Others in the 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 the result will be out within one minute), if successful, it will be smooth. If the upload fails, it will report that the upload failed. 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 remind you later.
So let us analyze this mechanism at a technical level:
When we click After pressing 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); ?>
Do 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 here The condition is the most critical part of the instant delivery mechanism:
status is greater than 1 (published) or equal to 1 (unpublished), (tips: statusThe default value is 1 when there are pictures), but the content posted by the current user can be read out. This has a very wonderful phenomenon, that is, no matter what, the Moments we post ourselves , you can always read it, but others may not (because if there are pictures, you need to call another process to upload the pictures, and then change the status to 2 in that process)
那么还有最后一个关键点,就是负责上传图片的那个进程(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。
最关键的是在显示的时候做了一些小技巧,让自己保证可以看到自己发的东西。
相关推荐:
The above is the detailed content of How to implement the instant delivery function of APP in php. For more information, please follow other related articles on the PHP Chinese website!