Spring boot使用websocket实现在线聊天

maven依赖

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</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-thymeleaf</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.26</version></dependency>

Java 类

WebSocket 配置

package com.zpjiang.chat;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;@Configuration
public class WebSocketConfig {/*** 	注入ServerEndpointExporter,* 	这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint*/@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}}

WebSocket 接口入径(ws://localhost:8087/socket/websocket/userId)

package com.zpjiang.chat;import java.util.concurrent.ConcurrentHashMap;
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.PathParam;
import javax.websocket.server.ServerEndpoint;import org.springframework.stereotype.Component;import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;@Component
@Slf4j
@ServerEndpoint("/websocket/{userId}") // 接口路径  ws://localhost:8087/socket/websocket/userId;
public class WebSocket {// 与某个客户端的连接会话,需要通过它来给客户端发送数据private Session session;/*** 用户ID*/private String userId;// concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。// 虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。// 注:底下WebSocket是当前类名private static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();// 用来存在线连接用户信息private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<String, Session>();/*** 链接成功调用的方法*/@OnOpenpublic void onOpen(Session session, @PathParam(value = "userId") String userId) {try {this.session = session;this.userId = userId;webSockets.add(this);sessionPool.put(userId, session);log.info("【websocket消息】有新的连接,总数为:" + webSockets.size());} catch (Exception e) {}}/*** 链接关闭调用的方法*/@OnClosepublic void onClose() {try {webSockets.remove(this);sessionPool.remove(this.userId);log.info("【websocket消息】连接断开,总数为:" + webSockets.size());} catch (Exception e) {}}/*** 收到客户端消息后调用的方法** @param message* @param session*/@OnMessagepublic void onMessage(String message) {log.info("【websocket消息】收到客户端消息:" + message);Message msg = JSONUtil.toBean(message, Message.class);sendOneMessage(msg.getToUID(), message);}/*** 发送错误时的处理* * @param session* @param error*/@OnErrorpublic void onError(Session session, Throwable error) {log.error("用户错误,原因:" + error.getMessage());error.printStackTrace();}// 此为广播消息public void sendAllMessage(String message) {log.info("【websocket消息】广播消息:" + message);for (WebSocket webSocket : webSockets) {try {if (webSocket.session.isOpen()) {webSocket.session.getAsyncRemote().sendText(message);}} catch (Exception e) {e.printStackTrace();}}}// 此为单点消息public void sendOneMessage(String userId, String message) {Session session = sessionPool.get(userId);if (session != null && session.isOpen()) {try {log.info("【websocket消息】 单点消息:" + message);session.getAsyncRemote().sendText(message);} catch (Exception e) {e.printStackTrace();}}}// 此为单点消息(多人)public void sendMoreMessage(String[] userIds, String message) {for (String userId : userIds) {Session session = sessionPool.get(userId);if (session != null && session.isOpen()) {try {log.info("【websocket消息】 单点消息:" + message);session.getAsyncRemote().sendText(message);} catch (Exception e) {e.printStackTrace();}}}}}

controller

package com.zpjiang.chat;import javax.annotation.Resource;import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;@RestController
public class WebsocketController {@Resourceprivate WebSocket webSocket;@GetMapping("/page")public ModelAndView page() {return new ModelAndView("webSocket");}@RequestMapping("/push/{toUID}/{msg}")public ResponseEntity<String> pushToClient(@PathVariable("msg")String message, @PathVariable("toUID") String toUID) throws Exception {webSocket.sendOneMessage(toUID, message);return ResponseEntity.ok("Send Success!");}
}

消息bean

package com.zpjiang.chat;import lombok.Data;
import lombok.Getter;
import lombok.Setter;@Data
@Getter
@Setter
public class Message {private String toUID;private String Msg;
}

main方法类

package com.zpjiang.chat;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ChatApplication {public static void main(String[] args) {SpringApplication.run(ChatApplication.class, args);}
}

配置文件

server:port: 8087servlet:context-path: /socketspring:application:name: websocket

视图文件 /src/main/resources/templates/webSocket.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebSocket消息通知</title>
</head>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>var socket;//打开WebSocketfunction openSocket() {if (typeof (WebSocket) === "undefined") {console.log("您的浏览器不支持WebSocket");} else {console.log("您的浏览器支持WebSocket");//实现化WebSocket对象,指定要连接的服务器地址与端口,建立连接.//ws://localhost:8087/socket/websocket/userIdvar socketUrl = "http://localhost:8087/socket/websocket/"+ $("#uid").val();//将https与http协议替换为ws协议socketUrl = socketUrl.replace("https", "ws").replace("http", "ws");console.log(socketUrl);if (socket != null) {socket.close();socket = null;}socket = new WebSocket(socketUrl);//打开事件socket.onopen = function() {console.log("WebSocket已打开");//socket.send("这是来自客户端的消息" + location.href + new Date());};//获得消息事件socket.onmessage = function(msg) {console.log(msg.data);//发现消息进入,开始处理前端触发逻辑};//关闭事件socket.onclose = function() {console.log("WebSocket已关闭");};//发生了错误事件socket.onerror = function() {console.log("WebSocket发生了错误");}}}var socket1;//打开WebSocketfunction openSocket1() {if (typeof (WebSocket) === "undefined") {console.log("您的浏览器不支持WebSocket");} else {console.log("您的浏览器支持WebSocket");//实现化WebSocket对象,指定要连接的服务器地址与端口,建立连接.//ws://localhost:8087/socket/websocket/userIdvar socketUrl = "http://localhost:8087/socket/websocket/"+ $("#toUID").val();//将https与http协议替换为ws协议socketUrl = socketUrl.replace("https", "ws").replace("http", "ws");console.log(socketUrl);if (socket1 != null) {socket1.close();socket1 = null;}socket1 = new WebSocket(socketUrl);//打开事件socket1.onopen = function() {console.log("WebSocket已打开");//socket.send("这是来自客户端的消息" + location.href + new Date());};//获得消息事件socket1.onmessage = function(msg) {console.log("socket1收到消息:");console.log(msg.data);//发现消息进入,开始处理前端触发逻辑};//关闭事件socket1.onclose = function() {console.log("WebSocket已关闭");};//发生了错误事件socket1.onerror = function() {console.log("WebSocket发生了错误");}}}//发送消息function sendMessage() {if (typeof (WebSocket) === "undefined") {console.log("您的浏览器不支持WebSocket");} else {console.log("您的浏览器支持WebSocket");console.log('{"toUID":"' + $("#toUID").val() + '","Msg":"'+ $("#msg").val() + '"}');socket.send('{"toUID":"' + $("#toUID").val() + '","Msg":"'+ $("#msg").val() + '"}');}}function sendMessage1() {if (typeof (WebSocket) === "undefined") {console.log("您的浏览器不支持WebSocket");} else {console.log("您的浏览器支持WebSocket");console.log('{"toUID":"' + $("#uid").val() + '","Msg":"'+ $("#msg").val() + '"}');socket1.send('{"toUID":"' + $("#uid").val() + '","Msg":"'+ $("#msg").val() + '"}');}}
</script>
<body><p>【uid】:<div><input id="uid" name="uid" type="text" value="1"></div><p>【toUID】:<div><input id="toUID" name="toUID" type="text" value="2"></div><p>【Msg】:<div><input id="msg" name="msg" type="text" value="hello WebSocket2"></div><p>【第一步操作:】:<div><button onclick="openSocket()">开启socket</button><button onclick="openSocket1()">开启socket2</button></div><p>【第二步操作:】:<div><button onclick="sendMessage()">发送消息</button><button onclick="sendMessage1()">发送消息2</button></div>
</body></html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/11396.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

软件2班20240513

第三次作业 package com.yanyu;import java.sql.*; import java.util.ResourceBundle;public class JDBCTest01 {public static void main(String[] args) {ResourceBundle bundle ResourceBundle.getBundle("com/resources/db");// ctrl alt vString driver …

高端智能终端RK3399主板在配送机器人项目的应用,支持免费定制

基于高端智能终端主板IoT-3399E推出了系列配送机器人&#xff0c;面向各类线下门店的配送服务。配送机器人可实现智能识别、精准配送、自动避障、自主调度系统、语音播报、信息互动等功能&#xff0c;大幅提升服务效率&#xff0c;为消费者带来更加便捷、智能的服务体验。 核心…

MySQL删除篇

文章目录 deletetruncatedrop delete delete from product ; 使用场景&#xff1a;一般正常删除数据&#xff0c;可回滚&#xff0c;不删除表结构 truncate truncate table product;使用场景:快速清空表数据&#xff0c;不可回滚&#xff0c;不删除表结构 drop drop tabl…

RK3568/RK3588智慧校园AI盒子方案,电子班牌,出入口管制,数字化校园等,支持鸿蒙,支持免费定制

智慧校园解决方案基于物联网、人工智能等新兴技术&#xff0c;为师生构建智能化校园环境&#xff0c;涵盖了校园安全、智慧教室、考勤管理、智慧食堂等多个场景&#xff0c;从工作、学习到生活&#xff0c;带来更人性化、更便捷、更高效的服务。 方案架构 方案特点 全面感知校园…

驾驭多云环境,加速AI创新丨Animbus Cloud 8.3.0 算力调度平台升级发布

大模型开启全球新一轮AI浪潮&#xff0c;伴随算力规模的爆发增长以及计算技术的多元创新&#xff0c;需要更稳定、高效、敏捷的异构计算基础设施&#xff0c;才能充分发挥对算力能力的重要支撑。 作为开放智能云边架构引领者&#xff0c;九州未来凭借多年的技术积累、实践沉淀…

2024-05-09 问AI: 在深度学习中,什么是模型检查点?

文心一言 在深度学习中&#xff0c;“模型检查点”&#xff08;Model Checkpoint&#xff09;是指训练过程中的一个保存点&#xff0c;它包含了模型在那一特定时刻的权重、配置和优化器的状态等信息。这种技术允许在训练被中断时&#xff0c;或者想要从某个点开始继续训练时&a…

JNI传递ByteArray参数错误

JNI传递ByteArray参数使用如下方式出现异常&#xff1a; jsize length env->GetArrayLength(data);jbyte* initBytes env->GetByteArrayElements(data, nullptr);char* initBuffer new char[length];memcpy(initBuffer, initBytes, length);env->ReleaseByteArrayE…

深入理解Python对象的字符串表示:`__str__`与`__repr__`的区别与应用

在Python编程中&#xff0c;我们经常需要将对象转换为字符串形式&#xff0c;无论是为了打印输出还是进行字符串操作。Python提供了两种特殊的方法来定义对象的字符串表示形式&#xff1a;__str__和__repr__。本文将详细讲解这两种方法的区别、用途以及如何在实际编程中使用它们…

BA112协议网关促进楼宇自控系统与OPC UA平台集成

在当今智能化建筑的潮流下&#xff0c;楼宇自控系统的发展已成为建筑行业的重要趋势。然而&#xff0c;由于不同厂商生产的楼宇自控设备使用的通信协议多样&#xff0c;导致了设备之间的互联互通存在困难。为了解决这一问题&#xff0c;钡铼技术最新推出了BACnet转OPC UA网关BA…

企业网站慎用免费SSL证书!OV证书才是首选

市面上有很多免费证书提供&#xff0c;免费的SSL证书更适用于个人博客、学生学习、测试等应用场景。如果您的网站是企业网站一定慎用免费的SSL证书&#xff0c;而是选择企业级的OV证书。 一&#xff0e;免费SSL证书的风险 1安全性&#xff1a;免费SSL证书通常只提供基本的加密…

景区AR导览导航语音播报小程序系统开发源码搭建

开发景区AR导览导航语音播报小程序系统需要以下步骤&#xff1a; 1. 确定需求&#xff1a;明确系统需要实现的功能&#xff0c;如AR导览、导航、语音播报等。 2. 设计系统架构&#xff1a;根据需求设计系统的架构&#xff0c;包括前端和后端。前端需要实现AR导览、导航、语音…

中学数学研究杂志中学数学研究杂志社中学数学研究编辑部2024年第4期目录

教学纵横 高中数学选择性必修课程函数主线分析 柳双;吴立宝; 1-4 贯彻新课程理念 促学习能力提升——以“三角函数诱导公式”教学为例 陆雨轩; 4-6《中学数学研究》投稿&#xff1a;cn7kantougao163.com 对高中数学新课标教材新增知识点的价值分析 钱伟风;刘瑞美; …

2024电商数据资料汇总

2024年跨境电商&#xff1a;连接全球市场的新纪元 随着全球数字化进程的不断推进&#xff0c;跨境电商已经成为了国际贸易的重要组成部分。2024年&#xff0c;跨境电商行业迎来了一系列挑战和机遇&#xff0c;塑造了全新的市场格局。 跨境电商市场规模的持续扩大 2024年&…

MASK-RCNN自定义数据集优化思路(pytorch)

MASK-RCNN自定义数据集优化思路(pytorch) 调整优化器GSD->AdamW也许有用不使用imagenet的标准化参数(std,mean),计算自己数据集的标准化参数传入进行训练.(如果模型照片不是通用图像也许表现会跟好)如果细长条的对象无法被准确识别,尝试调整AnchorGenerator的aspect_ratios…

嵌入式开发基础(一)-数据类型

数据类型 基本类型 int number ; /*定义整型变量*/ float fFloat; /*定义浮点型变量*/ char cChar; /*定义字符型变量*/ enum Fruits(Watermelon,Mango,Grape); /*定义枚举变量*/ 构…

postgresql允许外部访问

要允许PostgreSQL数据库接受外部连接&#xff0c;你需要通过编辑PostgreSQL的配置文件来修改其监听设置和访问控制规则。下面是具体的步骤&#xff1a; 1. 修改postgresql.conf文件 这个文件控制着PostgreSQL服务器的运行时行为&#xff0c;包括监听的地址。 定位配置文件&am…

微盟与管易云对接集成查询订单列表打通仓库新增

微盟与管易云对接集成查询订单列表打通仓库新增 数据源平台:微盟 微盟为众多商家提供海量应用与产品服务&#xff0c;并面向电商零售、商超生鲜、餐饮、跨境、美业等行业提供数字化升级解决方案。基于近10年商业实践&#xff0c;微盟构建了微盟WOS新商业操作系统&#xff0c;为…

大米自动化生产线揭秘:包装设备选择与维护之道

在现代化的大米生产过程中&#xff0c;自动化生产线的应用已经越来越广泛。其中&#xff0c;包装设备作为生产线上的重要一环&#xff0c;其选择与维护直接关系到产品的质量和生产效率。与星派一起探讨大米自动化生产线中包装设备的选择与维护之道。 一、包装设备的选择 在选择…

pycharm虚拟环境

File->setting->project->Python interpreter 路径必须写全

17、Flink 的 Checkpointing 配置详解

Checkpointing 1.概述 Flink 中的每个方法或算子都能够是有状态的&#xff0c;状态化的方法在处理单个 元素/事件 的时候存储数据&#xff0c;为了让状态容错&#xff0c;Flink 需要为状态添加 checkpoint&#xff08;检查点&#xff09;。 2.开启与配置 Checkpoint 默认 c…