SpringBoot集成websocket(Spring方式)
SpringWebSocketConfig配置
package com.meeno.chemical.socket.task.config;
import com.meeno.chemical.socket.task.handler.TaskProgressWebSocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
/**
* @description: SpringBoot WebSocket config
* @author: Wzq
* @create: 2020-07-16 10:50
*/
@Configuration
@EnableWebSocket
public class SpringWebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// 注册自定义消息处理,消息路径为`/ws/taskProgress`
registry.addHandler(new TaskProgressWebSocketHandler(),"/ws/taskProgress").setAllowedOrigins("*");
}
}
TaskProgressWebSocketHandler
package com.meeno.chemical.socket.task.handler;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* @description: 任务进度WebSocketHandler
* @author: Wzq
* @create: 2020-07-16 10:51
*/
public class TaskProgressWebSocketHandler extends TextWebSocketHandler {
/**
* concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。
*/
public final static ConcurrentHashMap<String, WebSocketSession> webSocketMap = new ConcurrentHashMap<String, WebSocketSession>();
/**
* 处理消息
* @param session
* @param message
* @throws Exception
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//在这里自定义消息处理...
TextMessage textMessage = new TextMessage("${message body}");
session.sendMessage(textMessage);
}
/**
* 连接建立后
* @param session
* @throws Exception
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
StandardWebSocketSession standardWebSocketSession = (StandardWebSocketSession) session;
List<String> taskNameList = standardWebSocketSession.getNativeSession().getRequestParameterMap().get("taskName");
String taskName = taskNameList.get(0);
//保存所有会话
webSocketMap.put(taskName,session);
}
/**
* 连接关闭后
* @param session
* @param status
* @throws Exception
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
if(!webSocketMap.isEmpty()){
// remove session after closed
WebSocketSession webSocketSession = webSocketMap.get(session.getId());
if(webSocketSession != null){
webSocketMap.remove(session.getId());
}
}
}
/**
* 发送消息给所有人
* @param content
*/
public static void sendMessageAll(String content){
webSocketMap.forEach((taskNameKey,session) ->{
TextMessage textMessage = new TextMessage(content);
try {
session.sendMessage(textMessage);
} catch (IOException e) {
e.printStackTrace();
}
});
}
/**
* 发送消息给指定某个任务
* @param taskName
* @param content
*/
public static void sendMessage(String taskName,String content){
webSocketMap.forEach((taskNameKey,session) ->{
if(taskName.equals(taskNameKey)){
TextMessage textMessage = new TextMessage(content);
try {
session.sendMessage(textMessage);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
问题
启用是会和定时任务冲突
需要配置
ScheduledConfig.java
package com.meeno.chemical.common.scheduling.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
* @description: 任务config
* @author: Wzq
* @create: 2020-07-16 11:05
*/
@Configuration
public class ScheduledConfig {
@Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.initialize();
return taskScheduler;
}
}
SpringBoot集成websocket(Spring方式)的更多相关文章
- springboot集成websocket的两种实现方式
WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...
- springboot集成drools的方式一
springboot集成drools的方式一(spring-drools.xml) 本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方 ...
- springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发
工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...
- springboot集成websocket实现大文件分块上传
遇到一个上传文件的问题,老大说使用http太慢了,因为http包含大量的请求头,刚好项目本身又集成了websocket,想着就用websocket来做文件上传. 相关技术 springboot web ...
- SpringBoot集成websocket发送后台日志到前台页面
业务需求 后台为一个采集系统,需要将采集过程中产生的日志实时发送到前台页面展示,以便了解采集过程. 技能点 SpringBoot 2.x websocket logback thymeleaf Rab ...
- SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送
代码全部复制,仅供自己学习用 1.环境搭建 因为在上一篇基于STOMP协议实现的WebSocket里已经有大概介绍过Web的基本情况了,所以在这篇就不多说了,我们直接进入正题吧,在SpringBoot ...
- SpringBoot集成websocket(java注解方式)
第一种:SpringBoot官网提供了一种websocket的集成方式 第二种:javax.websocket中提供了元注解的方式 下面讲解简单的第二种 添加依赖 <dependency> ...
- SpringBoot集成redis + spring cache
Spring Cache集成redis的运行原理: Spring缓存抽象模块通过CacheManager来创建.管理实际缓存组件,当SpringBoot应用程序引入spring-boot-starte ...
- springboot集成webSocket能启动,但是打包不了war
1.pom.xml少packing元素 https://www.cnblogs.com/zeussbook/p/10790339.html 2.SpringBoot项目中增加了WebSocket功能无 ...
随机推荐
- 利用PE破解系统密码
1.利用pe制作工具制作pe启动盘或者ios镜像 2.制作好后,在虚拟机设置里面加载镜像 3. 3.开启时选择打开电源进入固件 4.开启后依次选择:Boot--->CD-ROM Drive并按F ...
- 常见内部排序算法对比分析及C++ 实现代码
内部排序是指在排序期间数据元素全部存放在内存的排序.外部排序是指在排序期间全部元素的个数过多,不能同时存放在内存,必须根据排序过程的要求,不断在内存和外存之间移动的排序.本次主要介绍常见的内部排序算法 ...
- python numpy 数据集合操作函数
arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])arr1array([0, 1, 2, 3, 4])np.intersect1d(arr,arr1)#计算数组ARR A ...
- 结对开发_石家庄地铁查询web系统
结对开发:队友田昕可 大二上学期做过只有两号线的地铁查询系统,但是只能在控制台操作.这一次将线路加到了六条,并且要求web实现,下面简述一下设计思路和具体代码实现: 1.数据库建表 于我们自己习惯而言 ...
- [刘阳Java]_第一个Java程序_第7讲
1. 其实第一个Java程序是很简单,但是当自己编写第一个Java程序时候需要注意如下几个内容: 理解Java程序的运行环境 校验你的Java环境变量是否能够运行你所写的第一个Java程序 理解Jav ...
- 高性能内存图数据库RedisGraph(三)
这篇文章,我将介绍截止目前,RedisGraph的最新版本(v2.4)对Cypher语言的支持情况. 1.模式(patterns) RedisGraph已支持Cypher中所有的模式. 2.类型(ty ...
- 每天五分钟Go - 闭包
闭包的示例代码 func getSequence() func() int{ i:=0 return func() int { i+=1 return i } } 首先,函数名getSequence, ...
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【二】
HarmonyOS内核开发-信号量开发案例学习记录 一.LiteOS里面的任务管理介绍: 任务状态通常分为以下四种: 就绪(Ready):该任务在就绪列表中,只等待CPU. 运行(Running) ...
- Qt 入门 ---- 布局管理
这是运行后的程序界面: 这是点击右上角"最大化"之后的程序界面: 接下来讲一下如何进行自动布局解决窗口拉伸问题. ① 原理: 在项目"设计"模式的左侧有如下两个 ...
- python 接口测试之 图片识别
4.1 pytesser安装 2.安装pytesser,下载地址:http://code.google.com/p/pytesser/ ,下载后直接将其解压到项目代码下,或者解压到python安装目录 ...