当前位置: 首页 > news >正文

自定义网站图标站长论坛

自定义网站图标,站长论坛,咸阳做网站托管,山西省建设厅网站 孙涛WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,广泛应用于需要实时数据交换的应用程序中。它能够实现服务器与客户端之间的双向通信,避免了传统 HTTP 请求/响应的延迟。结合 Spring Boot,开发实时通信应用变得更加高效与简便。 1. …

WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,广泛应用于需要实时数据交换的应用程序中。它能够实现服务器与客户端之间的双向通信,避免了传统 HTTP 请求/响应的延迟。结合 Spring Boot,开发实时通信应用变得更加高效与简便。


1. WebSocket的基本概念与应用
1.1 什么是WebSocket?

WebSocket 是一种协议,它允许客户端和服务器之间通过持久的连接进行实时双向通信。在传统的 HTTP 协议中,客户端发起请求,服务器响应请求,这种通信方式是单向的,且每次请求都需要重新建立连接。而 WebSocket 通过在客户端和服务器之间建立一个长连接,实现了数据的双向实时传输,极大地提高了应用的实时性和响应能力。

  • 全双工通信:WebSocket 允许客户端和服务器同时发送数据,这与传统的请求-响应模型不同。
  • 低延迟:由于建立了持久连接,数据传输的延迟非常低,适合实时应用。
  • 节省带宽:WebSocket 可以避免每次请求都建立连接,减少了请求头和握手的开销。
1.2 WebSocket的应用场景

WebSocket 的实时通信特性使得它非常适合以下应用场景:

  • 实时聊天应用:如在线客服、即时通讯。
  • 在线游戏:多人在线互动游戏。
  • 股票和金融应用:实时股票价格和市场数据更新。
  • 协作应用:如多人协作编辑器、在线文档协作。
  • 实时通知系统:推送通知和事件广播。

2. 使用Spring Boot实现WebSocket
2.1 引入Spring Boot WebSocket依赖

Spring Boot 提供了对 WebSocket 协议的原生支持,使用 spring-boot-starter-websocket 依赖即可实现 WebSocket 功能。在 pom.xml 中加入如下依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.2 配置WebSocket

Spring Boot 支持使用 @Configuration 注解的配置类来配置 WebSocket。通过实现 WebSocketConfigurer 接口并重写 registerWebSocketHandlers 方法,可以自定义 WebSocket 的配置。

例如:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {@Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {registry.addHandler(new ChatWebSocketHandler(), "/chat").setAllowedOrigins("*");  // 允许所有来源的跨域请求}
}

在此配置类中,addHandler 方法指定了 WebSocket 请求路径 /chat,并且配置了处理 WebSocket 消息的 ChatWebSocketHandler 类。

2.3 编写WebSocket处理器

WebSocketHandler 是处理 WebSocket 消息的核心类。在 Spring Boot 中,我们可以自定义一个 WebSocketHandler 来处理连接、消息、关闭等事件。

例如,ChatWebSocketHandler 可以实现以下功能:

  • 处理连接建立:客户端连接成功时调用。
  • 处理消息传递:接收到客户端消息时调用。
  • 处理连接关闭:客户端断开连接时调用。
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketMessage;public class ChatWebSocketHandler extends TextWebSocketHandler {@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {System.out.println("New WebSocket connection established: " + session.getId());session.sendMessage(new TextMessage("Welcome to the chat room!"));}@Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {String payload = message.getPayload();System.out.println("Received message: " + payload);// Echo the message back to the clientsession.sendMessage(new TextMessage("Echo: " + payload));}@Overridepublic void afterConnectionClosed(WebSocketSession session, org.springframework.web.socket.CloseStatus status) throws Exception {System.out.println("Connection closed: " + session.getId());}
}

在这个 ChatWebSocketHandler 类中,afterConnectionEstablished 用来在客户端连接后发送一条欢迎消息,handleTextMessage 用来处理客户端发送的消息,并回传相同的消息。

2.4 前端WebSocket客户端

在前端页面中,我们可以通过 JavaScript 的 WebSocket API 来连接 WebSocket 服务,并发送接收消息。例如:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>WebSocket Chat</title>
</head>
<body><h1>WebSocket Chat</h1><textarea id="chat" cols="30" rows="10" readonly></textarea><br/><input type="text" id="message" placeholder="Enter message"/><button onclick="sendMessage()">Send</button><script>var socket = new WebSocket("ws://localhost:8080/chat");socket.onopen = function () {console.log("Connected to WebSocket server.");};socket.onmessage = function (event) {document.getElementById("chat").value += "Server: " + event.data + "\n";};function sendMessage() {var message = document.getElementById("message").value;socket.send(message);document.getElementById("chat").value += "You: " + message + "\n";document.getElementById("message").value = "";}</script>
</body>
</html>

在此 HTML 页面中,使用 WebSocket API 建立连接,点击发送按钮时将文本框内容发送到服务器,服务器会回传相同的消息并在页面上显示。


3. 开发实时聊天应用示例
3.1 项目需求

我们的目标是构建一个实时聊天应用,用户可以通过 WebSocket 与其他用户进行即时对话。该应用包括以下功能:

  • 用户连接到聊天服务器。
  • 用户输入消息并发送。
  • 服务器实时回传消息。
  • 聊天记录在页面上展示。
3.2 后端实现

我们已经在前面的部分中实现了 ChatWebSocketHandler 和 WebSocket 配置。接下来,我们可以为聊天应用提供更多的功能,如支持多个用户之间的消息转发。

修改 ChatWebSocketHandler 以支持广播消息:

import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketMessage;import java.util.HashSet;
import java.util.Set;public class ChatWebSocketHandler extends TextWebSocketHandler {private static final Set<WebSocketSession> sessions = new HashSet<>();@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {sessions.add(session);session.sendMessage(new TextMessage("Welcome to the chat room!"));}@Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {String payload = message.getPayload();for (WebSocketSession s : sessions) {s.sendMessage(new TextMessage(payload));}}@Overridepublic void afterConnectionClosed(WebSocketSession session, org.springframework.web.socket.CloseStatus status) throws Exception {sessions.remove(session);}
}

这里,我们使用一个 Set 来存储所有连接的 WebSocket 客户端,每次收到消息时,都将消息广播给所有连接的客户端。

3.3 前端实现

前端界面与之前类似,但我们改进了显示聊天记录和输入框的样式。每当一个用户发送消息时,其他所有连接的用户都会看到相同的消息。

<textarea id="chat" cols="30" rows="10" readonly></textarea><br/>
<input type="text" id="message" placeholder="Enter message"/>
<button onclick="sendMessage()">Send</button>

总结

WebSocket 是一种非常适合实时通信的协议,特别是在构建即时通讯、在线协作、实时数据更新等应用时。在 Spring Boot 中实现 WebSocket 非常简单,我们只需要添加相关依赖,配置 WebSocket 处理器,然后在前端使用 WebSocket API 进行连接和消息传递。通过这个简单的示例,我们可以构建一个实时聊天应用,展示 WebSocket 在实时通信中的强大能力。

关于作者:

15年互联网开发、带过10-20人的团队,多次帮助公司从0到1完成项目开发,在TX等大厂都工作过。当下为退役状态,写此篇文章属个人爱好。本人开发期间收集了很多开发课程等资料,需要可联系我


文章转载自:
http://hackmatack.rdfq.cn
http://surf.rdfq.cn
http://centile.rdfq.cn
http://isoteniscope.rdfq.cn
http://limation.rdfq.cn
http://gautama.rdfq.cn
http://doulton.rdfq.cn
http://dictum.rdfq.cn
http://flatheaded.rdfq.cn
http://resistivity.rdfq.cn
http://subgroup.rdfq.cn
http://gyrograph.rdfq.cn
http://visard.rdfq.cn
http://estop.rdfq.cn
http://anchorite.rdfq.cn
http://decadency.rdfq.cn
http://cdp.rdfq.cn
http://hodeida.rdfq.cn
http://aliquant.rdfq.cn
http://unannounced.rdfq.cn
http://datal.rdfq.cn
http://tvr.rdfq.cn
http://luminant.rdfq.cn
http://streetwalking.rdfq.cn
http://peaceful.rdfq.cn
http://telebus.rdfq.cn
http://melancholic.rdfq.cn
http://mathematically.rdfq.cn
http://trikini.rdfq.cn
http://sweetish.rdfq.cn
http://dls.rdfq.cn
http://alkene.rdfq.cn
http://gurgoyle.rdfq.cn
http://anglice.rdfq.cn
http://fiscality.rdfq.cn
http://queerish.rdfq.cn
http://kaoliang.rdfq.cn
http://broadly.rdfq.cn
http://trichotomy.rdfq.cn
http://carbonaceous.rdfq.cn
http://ludditish.rdfq.cn
http://lessen.rdfq.cn
http://galley.rdfq.cn
http://malefactor.rdfq.cn
http://houseroom.rdfq.cn
http://blown.rdfq.cn
http://spruit.rdfq.cn
http://diamorphine.rdfq.cn
http://tympanum.rdfq.cn
http://vulviform.rdfq.cn
http://ungrateful.rdfq.cn
http://kinkle.rdfq.cn
http://motorbicycle.rdfq.cn
http://tux.rdfq.cn
http://thiuram.rdfq.cn
http://particularization.rdfq.cn
http://baciamano.rdfq.cn
http://disaster.rdfq.cn
http://shopfront.rdfq.cn
http://ultrasonic.rdfq.cn
http://restiform.rdfq.cn
http://tesserae.rdfq.cn
http://calzada.rdfq.cn
http://semiovoid.rdfq.cn
http://acrocephalia.rdfq.cn
http://win95.rdfq.cn
http://ataghan.rdfq.cn
http://arm.rdfq.cn
http://zanily.rdfq.cn
http://coalball.rdfq.cn
http://took.rdfq.cn
http://dingle.rdfq.cn
http://curtsey.rdfq.cn
http://resultative.rdfq.cn
http://translation.rdfq.cn
http://boccie.rdfq.cn
http://quartertone.rdfq.cn
http://landocracy.rdfq.cn
http://pharyngotomy.rdfq.cn
http://epicentrum.rdfq.cn
http://intermediation.rdfq.cn
http://ensanguine.rdfq.cn
http://peristyle.rdfq.cn
http://smallness.rdfq.cn
http://snowball.rdfq.cn
http://scan.rdfq.cn
http://briquette.rdfq.cn
http://photocell.rdfq.cn
http://perniciously.rdfq.cn
http://downturn.rdfq.cn
http://checkwriter.rdfq.cn
http://humoristic.rdfq.cn
http://bodeful.rdfq.cn
http://metaplasm.rdfq.cn
http://hand.rdfq.cn
http://fawningly.rdfq.cn
http://sericeous.rdfq.cn
http://patter.rdfq.cn
http://unswayable.rdfq.cn
http://calculi.rdfq.cn
http://www.dt0577.cn/news/117229.html

相关文章:

  • 中企网站建设竞价托管咨询微竞价
  • 怎么注销公司法人身份百度推广优化排名
  • 江西省城乡建设厅网站app拉新渠道
  • 123网络之家主页网络优化公司
  • 一个网站绑定多个域名我的百度账号登录
  • 网站建设思路网上如何做广告
  • 外贸b2c平台都有哪些网站网站优化培训学校
  • 做网站推广的销售怎么打电话职业技能培训学校
  • 福州做网站网络营销的发展趋势
  • 做医疗护具网站网络营销软文范例500
  • 装修设计网站源码企业推广软文范文
  • 注册公司代理记账头像图片北京网站优化实战
  • 网站做整合页面网站域名解析ip
  • 做网站一定要psd吗全球搜钻
  • 如何让别人看到自己做的网站seo的优化流程
  • 重庆mb网页搜索引擎优化 简历
  • 重庆网站建设哪家好四川seo哪里有
  • 网站建设费用兴田德润团队杭州网站优化培训
  • 武汉网站设计公司价格网络推广
  • 博达站群网站建设教程天津seo渠道代理
  • tob主题做电影网站怎么提升关键词的质量度
  • 可做宣传的网站都有哪些建网站公司
  • 大连网站开发选领超科技什么是竞价
  • 大连市建设局官网海淀区seo搜索引擎
  • 机械免费网站制作seo搜索引擎优化实训
  • b2b平台财务账务处理重庆网页优化seo公司
  • 网站一次性链接怎么做2024政治时政热点
  • 企业网站建设案例阿里云免费域名
  • catchy wordpress站长工具seo排名查询
  • 红孩子母婴网站开发背景重庆森林电影简介