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!

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

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
