网页消息系统是如何做到不刷新网页提醒的?
比如像segmentfault这样的网站,怎么做的的?
回复内容:
比如像segmentfault这样的网站,怎么做的的?
可以很直白的回答你,这个是用Ajax的技术做的。首先我们需要了解一下AJAX是什么:
使用Ajax的最大优点,就是能在不更新整个页面的前提下维护数据。这使得Web应用程序更为迅捷地回应用户动作,并避免了在网络上发送那些没有改变的信息。 —— AJAX
看了这句话你就应该明白,这正是你所寻求的的东西。下面我们就来写一个简单的示例,获取Segmentfault官方微博的消息,简单的了解AJAX的强大(jQuery版)
http://jsfiddle.net/AXVrT/
至于具体如何AJAX,怎么样AJAX,我希望你能自行Google,网上很多教程,多看看就会了。
大多使用Long Polling来替代传统的ajax轮询
Long Polling原理其实很简单,也很讨巧。与Polling相比,Long Polling客户端也许不会马上收到来自服务端的响应,需要等待一些时间(直到有新消息,或者连接timeout了等等)。同样的,客户端也不再需要定时向服务发送请求了,而是直到收到服务端响应之后,或者连接丢失之后,客户端接着马上请求客户端。这里,我打个比方,传统的Polling一般是由C向S询问:”有我的信件吗?”。S接到询问之后,会立即查询,并且把查询结果告诉C,不管有没有C的信件,要码回复:”嗯,你有X封信。”,要码回复:”没,没有你的信”.而Long Polling更像是这样,C向S发出询问:”有我的信件吗?”,S开始查询,如果有则回复C:”嗯,有你x封信”。如果没有,则不作任何回复,而是让C等着,自己一遍一遍地查询是否有订阅者的信。换句话说:当S收到C的查询请求之后,Polling则只查询一次,并且把查询结果告诉C;而Long Polling收到请求之后,则会一遍一遍地查询,直到有消息才会响应C,不然一直hold Client。
详情:PHP服务端推送技术Long Polling
AJAX只能做到轮询,接收消息有延迟。 WebSockets可以实时收到消息。

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
