PHP server push technology chat room
保存用户提交的聊天内容 简易版本:
主要的HTTP长连接部分,chat_content.php文件:
Code description: 06. Set a timeout. Since you need to maintain a long HTTP connection, this time must be longer. It may take several hours. The article mentioned above also explains that only two such long HTTP connections can be opened. Due to browsing device limitations. in addition In fact, even if you set a never timeout, the configuration file of the server part (such as Apache) may also set the maximum waiting time for HTTP requests, so the effect may not be what you think. Generally, the default may be 15 minutes. time out. if If you are interested, you can try to modify it yourself. 09. A section of blank space is output here, mainly because the manual has explained that the IE browser will not directly output the first 256 characters, so we first output some blank space casually to allow the subsequent content to be output, and possibly other Browsers also have other For browser settings, you can check the description of the frush function in the PHP manual for details. The next 11 and 12 lines are to force these whitespace characters to be output by the browser. 13. ~ 20. The main purpose here is to calculate the number of file lines so that the content can be read from the end of this line. The following while loop is an infinite loop, which is to output the file content in a loop. Each time it is judged whether it has reached the end of the file. If a user writes to the file, the current detection is definitely not the end of the file, so the line is read and output. Otherwise it will refer to The needle moves forward one line and continues to cycle, waiting 1000 microseconds each time, 39. If a long connection is maintained, even if the client is disconnected, the server may not know that the client has been disconnected, so some heartbeat records may be needed here, such as each user keeping a heartbeat flag, each grid Update in a few seconds The last heartbeat time, when the last time detected has not been updated for a long time, this infinite loop is launched and the HTTP connection is closed. Demo Example 2: Traditional B/S structure applications all use "client pull" to achieve data exchange between the client and the server. This article will implement a simple idea of a server-pushed PHP chat room by combining Ticks. PHPer, especially those who have used set_cookie, header, must have seen this prompt message: "Warning: Cannot modify header information - headers already sent by...", this is because communication is through the HTTP protocol , the data packet will contain two parts, one is Header and the other is data. Generally speaking, the Header part is started first, and the length of the Data part is specified in the Header part, and then \r\n\r\n is used to indicate the end of the header part, followed by the Data part. When there is any output, the Header part is sent. At this time, if you use the header function to change some domain information of the Header part, you will get the above prompt information. A simple solution is to use output_buffering. Let it cache the server's output and don't send the header part to the client too early. So, if output_buffering is not used, can it be achieved that whenever the server has output, it is immediately sent to the client? Do the following experiment:
It turns out that you still have to wait until the script is fully executed before you can see everything at once the result of. why? This is because it only solves the caching problem, but there is also a buffering problem. PHP will buffer the output of the program. Therefore, you still need to call flush() at this time to force PHP to send all program output to the client.
Have you seen that the server data is constantly being displayed? There are relationships between several concepts, and I’ll add them here: Using ob_start() in the code is equivalent to using output_buffering=on in php.ini, using the server cache. Using ob_end_flush() in the code is equivalent to using output_buffering = false in php.ini to turn off the server cache. 1 2 Next Page Last Page |

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

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:
