news 2026/8/28 1:34:46

简单的Websocket程序示例(Spring Boot)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
简单的Websocket程序示例(Spring Boot)

该示例的工程代码我已经上传到CSDN资源网站,下载地址是:
https://download.csdn.net/download/look4liming/10957504

1、首先建一个空的Spring Boot项目
2、在pom.xml中添加如下依赖:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>

3、新增一个Configuration类
注意,这个类必须有,否则Websocket服务类的注解配置无法生效。

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * 必须有此类,否则Websocket配置不生效(原因尚未查清)。 * @author Bright Lee */ @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }

4、新增Websocket服务类

import java.io.IOException; 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; import org.springframework.stereotype.Component; @ServerEndpoint(value = "/websocket") @Component public class WebSocketServer { private Session session; @OnOpen public void onOpen(Session session) { this.session = session; try { sendMessage("连接成功!!!"); } catch (IOException e) { e.printStackTrace(); } } @OnClose public void onClose() { } @OnMessage public void onMessage(String message, Session session) { try { sendMessage("您发送的消息是--->" + message); } catch (IOException e) { e.printStackTrace(); } } @OnError public void onError(Session session, Throwable error) { error.printStackTrace(); } public void sendMessage(String message) throws IOException { session.getBasicRemote().sendText(message); } }

5、新增index.html

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Websocket客户端</title> <script> var websocket = null; var content = null; function init() { content = document.getElementById("content"); } function printToScreen(message) { var pElement = document.createElement("p"); pElement.innerHTML = message; content.appendChild(pElement); } function connect() { if (websocket != null) { return; } var url = "ws://127.0.0.1:8080/websocket"; printToScreen("开始连接:" + url); websocket = new WebSocket(url); websocket.onopen = function(event) { printToScreen("连接成功!"); } websocket.onmessage = function(event) { printToScreen("收到消息:" + event.data); } websocket.onerror = function(event) { printToScreen("出现错误:" + event.data); closeWebsocket(); } } function sendMessage() { if (websocket == null) { return; } websocket.send(messageInput.value); printToScreen("发送消息:" + messageInput.value); } function closeWebsocket() { if (websocket == null) { return; } websocket.close(); websocket = null; printToScreen("已关闭!"); } </script> </head> <body onload="init();"> <div style="text-align: left;"> <form action=""> <input type="text" id="messageInput" value="你好!!!"> <input type="button" onclick="connect();" value="连接"> <input type="button" onclick="sendMessage();" value="发送"> <input type="button" onclick="closeWebsocket();" value="关闭"> <br /> </form> </div> <div id="content"></div> </body> </html>

6、启动Spring Boot项目,访问index.html
在浏览器中输入:http://127.0.0.1:8080/index.html

青蛙客服系统:https://download.csdn.net/download/look4liming/93326820

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/28 1:34:35

AI PC与智慧家庭融合:本地推理如何重构智能家居场景

这次我们来看一个不太像“软件项目”、但未来很可能决定 AI 应用形态的合作新闻&#xff1a;联想集团与海尔集团签署战略合作协议&#xff0c;核心方向是把联想 AI PC 与海尔智慧家庭场景融合。放在技术视角下&#xff0c;这条消息不是在说“两家大厂互相站台”&#xff0c;而是…

作者头像 李华
网站建设 2026/8/28 1:34:33

GPS信号为何脆弱?从1瓦干扰到航空安全的技术拆解

军用 GPS 干扰到底有多强&#xff1f;一个 1 瓦的干扰设备放在 1 公里外&#xff0c;到达接收机天线口面的干扰功率约 -66 dBm&#xff0c;而 GPS 卫星信号到达地面的典型功率只有 -128 dBm 左右。双方的“信干比”超过 60 dB&#xff0c;绝大多数商用 GPS 接收机的抗干扰容限只…

作者头像 李华
网站建设 2026/8/28 1:31:09

基于SpringBoot的民间艺术传承管理系统(源码+讲解视频+LW)

温馨提示&#xff1a;本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;本人主页置顶文章(点我)开头有 CSDN 平台…

作者头像 李华
网站建设 2026/8/28 1:29:38

全栈接口迁移怎样平稳推进

全栈接口迁移怎样平稳推进从 Node.js REST API 迁到 GraphQL&#xff0c;不是只换一份接口描述。旧接口可能把校验、权限、缓存和字段默认值藏在控制器里&#xff1b;GraphQL 允许客户端组合字段&#xff0c;查询成本和错误语义都会变化。若同时加入 AI 预测字段&#xff0c;模…

作者头像 李华
网站建设 2026/8/28 1:29:20

YOLOv5实战:冬虫夏草小目标检测从训练到部署全流程

简介&#xff1a;目标检测是计算机视觉领域的核心任务之一&#xff0c;其本质是在图像中定位并识别感兴趣的目标。在实际工程中&#xff0c;针对小目标、复杂背景的检测场景&#xff0c;模型的选择与训练策略往往比网络结构本身更关键。YOLOv5作为成熟稳定的检测框架&#xff0…

作者头像 李华
网站建设 2026/8/28 1:28:55

基于微信小程序与Java Spring Boot的学生签到系统设计与实现

简介&#xff1a;在移动互联网与教育信息化深度融合的背景下&#xff0c;Web应用开发与前后端分离架构已成为构建现代管理系统的核心技术范式。其原理在于通过清晰的职责划分&#xff0c;前端负责用户交互与界面渲染&#xff0c;后端专注业务逻辑与数据持久化&#xff0c;二者通…

作者头像 李华