HTTP持久通讯WebSocket使用

WebSocket是HTML5新增的通讯技术,用于实现客户端与服务器的双向、持久连接。常见应用场景如实时通知。相比于Ajax轮循,WebSocket能有效降低延迟,提高效率。建立WebSocket连接时,客户端先通过HTTP请求携带Upgrade头信息向服务器申请升级协议,服务器确认后即建立连接。WebSocket协议的优点在于服务器可以主动向客户端推送数据,适合需要实时交互的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

websocket是h5才有的一种通讯技术,websocket使客户端(浏览器)与服务器建立持久通讯。

引入一个应用场景
这里写图片描述
这是微博的左侧通知栏目,当你在看微博的时候,如果有人发了微博,这里会实时自动更新未阅读的微博个数。怎么才可以做到呢?

办法一: Ajax轮循

使用死循环,和ajax对后台数据无线轮循访问,如果有新的更新,这里会自动增加。

办法二 H5的WebSocket

WebSocket是一个基于TCP协议的协议。可以建立持久通讯,与后台实时交互,更新前台数据。

建立过程:

客户端首先通过Http协议给后台发送请求,这个请求和通常的 HTTP 请求不同,包含了一些附加头信息,其中附加头信息”Upgrade: WebSocket”表明这是一个申请协议升级的 HTTP 请求,服务器接收请求之后,经过解读,如果支持WebSocket协议,给客户端发出相应,这个WebSocket持久通讯就建立起来了。

优点:

常规的客户端与服务端交互,首先是客户端有request请求,然后服务器才会有response的相应。request和response是一个完整的过程,并且response不能单独存在,也就是说,服务器端不能主动与客户端通讯,只有客户端发出了请求,服务端才可以回应response。WebSocket正好弥补了这个通讯的缺点,它可以主动与客户端进行通讯。

程序实现:

maven引入包

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

编写服务器WebSocketTest.java

package com.test;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

/**
 * @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
 *                 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
 */
HTML 5 Web Sockets is a powerful and effective technique for real-time information processing. There exists many techniques such as Poling, Long Poling and Streaming that are said to be better earier days. With web sockets, it shows a better outcome for the end user as well as a proper utilization of the server bandwidth. WebSocket is a web technology providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API for in Web IDL is being standardized by the W3C.WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. The WebSocket protocol makes possible more interaction between a browser and a web site, facilitating live content and the creation of real-time games. This is made possible by providing a standardized way for the server to send content to the browser without being solicited by the client, and allowing for messages to be passed back and forth while keeping the connection open. In this way a two-way (bi-directional) ongoing conversation can take place between a browser and the server. A similar effect has been achieved in non-standardized ways using stop-gap technologies such as Comet.In addition, the communications are done over TCP port number 80, which is of benefit for those environments which block non-standard Internet connections using a firewall. WebSocket protocol is currently supported in several browsers including Google Chrome, Internet Explorer, Firefox, Safari and Opera. WebSocket also requires web applications on the server to support it. Here goes a comparison of polling vs Web Sockets.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值