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功能无 ...
随机推荐
- 物理机连接虚拟机中的数据库及Windows添加防火墙允许端口详细操作步骤
公司项目中因为会使用到SQL server数据库,但是自己电脑无论安装2008R2或者2014版本都不成功,我想可能是和之前安装的一些Windows的软件存在冲突. 于是便单独创建了一台虚拟机,在虚拟 ...
- java001-泛型
泛型出现的意义: 为编码阶段的不确定性和转化做视觉设计 将运行期遇到的问题转移到编译期,省去了强转的麻烦 package com.xiaolin.basic; /** * 泛型:将运行期遇到的问题转移 ...
- UI自动化测试框架Gauge 碰到无法识别Undefined Steps 红色波纹标记
如果碰到无法识别的情况,例如下面的红色波纹,可以试一下: 第一步: 第二步: 不勾选'offline work' 第三部:刷新之后可以重新编译.
- python 最大公约数 最小公倍数
def gongyueshu(m,n): if m<n: m,n=n,m elif m==n: return m if m/n==int(m/n): return n else: for i i ...
- 00JAVA语法基础_六位验证码 01
在网上看了许多的源程序,涉及到的东西也不太一样,多了图形处理的,由于还没理解太明白,只是做了控制台. package Six_Code; import java.util.Random; import ...
- 无需kubectl!快速使用Prometheus监控Etcd
在本文中,我们将安装一个Etcd集群并使用Prometheus和Grafana配置监控,以上这些操作我们都通过Rancher进行. 我们将看到在不需要依赖的情况下充分利用Rancher的应用商店实现这 ...
- 【Tips】IDEA中自己常用的快捷键
在idea中自己常用的一些快捷键 alt + i; //向上: alt + k; //向下: alt + j; //向左: alt + l; //向右: alt + o; //移至行首: alt + ...
- HDFS学习总结之API交互
第一种.shell交互 官方文档:http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0-cdh5.7.0/hadoop-project-dist/ha ...
- [考试总结]noip模拟22
又发现模拟 \(22\) 的总结也咕掉了,现在补上它... 似乎又是gg的一场. 以为自己的部分分数打的很全,然而到后面发现自己的树剖打假了 \(\color{green}{\huge{\text{树 ...
- SQL_之 递归_START WITH id ='102' CONNECT BY PRIOR pid=id
oracle 递归用法 SELECT * FROM menu START WITH id ='102' CONNECT BY PRIOR pid=id 一种应用 SELECT * FROM menu ...