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 {@Overridepublic 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*/@Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {//在这里自定义消息处理...TextMessage textMessage = new TextMessage("${message body}");session.sendMessage(textMessage);}/*** 连接建立后* @param session* @throws Exception*/@Overridepublic 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*/@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {if(!webSocketMap.isEmpty()){// remove session after closedWebSocketSession 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 {@Beanpublic TaskScheduler taskScheduler(){ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();taskScheduler.setPoolSize(10);taskScheduler.initialize();return taskScheduler;}}

SpringBoot集成websocket(Spring方式)相关推荐

  1. springboot集成webSocket实现实时推送

    springboot集成webSocket实现实时推送 webSocket实现推送 webSocket是什么? 需求说明 websocket集成步骤 pom.xml webSocket实现 自定义处理 ...

  2. SpringBoot集成WebSocket

    一.什么是WebSocket WebSocket是一种网络传输协议,位于OSI模型的应用层.可在单个TCP连接上进行全双工通信,能更好的节省服务器资源和带宽并达到实时通讯. 客户端和服务端只需完成一次 ...

  3. Springboot集成websocket实现消息推送和在线用户统计

    一.HTTP 说到websocket首先要说Http,Http大家都知道是一个网络通信协议,每当客户端浏览器需要访问后台时都会发一个请求,服务器给出响应后该连接就会关闭,请求只能有客户端发起,服务端是 ...

  4. SpringBoot集成WebSocket实现及时通讯聊天功能!!!

    1:在SpringBoot的pom.xml文件里添加依赖: <!-- websocket --> <dependency><groupId>org.springfr ...

  5. SpringBoot集成websocket能力(stomp)

    序 之前有分享过springBoot集成Websocket推送信息.今天主要是来继续分享升级版,这次是采用STOMP协议.用这个的好处有很多,比如可以屏蔽浏览器之间的差异,更方便对接消息中间件等. 一 ...

  6. springboot集成websocket(一)客户端、客户端断线重连、客户端连接验证

    springboot集成websocket客户端 一.首先是导入依赖包 1.在pom.xml中加入下述即可 <!--websocket作为客户端 --><dependency> ...

  7. SpringBoot——SpringBoot集成WebSocket实现简单的多人聊天室

    文章目录: 1.什么是WebSocket? 2.Java中的WebSocket API 2.1 WebSocket开发中的相关注解及API方法 2.2 前端技术对WebSocket的支持 3.多人聊天 ...

  8. SpringBoot 集成 WebSocket,实现后台向前端推送信息

    作者 | 大树先生 来源 | https://blog.csdn.net/MacWx/article/details/111319558 前言 在一次项目开发中,使用到了Netty网络应用框架,以及M ...

  9. SpringBoot集成WebSocket,实现后台向前端推送信息

    作者 | 大树先生 来源 | https://blog.csdn.net/MacWx/article/details/111319558 前言 在一次项目开发中,使用到了Netty网络应用框架,以及M ...

最新文章

  1. throws throw 自定义异常
  2. python中的及||
  3. sklearn中的验证
  4. 作者:王玲玲(1978-),女,中国科学院上海天文台高级工程师
  5. 《极品飞车12》官方网站公布发售日
  6. 小米10之后摩托罗拉Edge+也要用一亿像素相机,还有3.5耳机孔
  7. PSIM软件学习---08 C程序块的调用
  8. 遍历界面控件 android,Android 判断所有字段是否已经输入的实例
  9. 前端 new实例后销毁实例_后浇带怎么做不漏浆?看个实例
  10. 【感悟】人生本如梦,学会看淡一切。
  11. 直接修改gba_gba修改教程完全版
  12. win10专业版/企业版怎样永久激活并查看激活期限
  13. node.js 上传文件比较 busboy vs. formidable vs. multer vs. multiparty
  14. JAVA基础算法(6)----- 国际象棋 α 皇后问题
  15. 陈强教授《机器学习及R应用》课程 第十五章作业
  16. C#,欧拉常数(Euler Constant)的算法与源代码
  17. Win10系统更新卡住了怎么办?如何修复?
  18. SQL零基础入门学习(八)
  19. Error instantiating servlet class 的解决办法
  20. debain系统安装nginx

热门文章

  1. 今晚中科院刘永鑫报告:宏基因组数据分析的机遇与挑战
  2. 宏基因组数据提交GSA实操手册—发表文章前必备技能
  3. ggplot2笔记5:通过图层构建图像
  4. 微信界面代码android,仿微信界面代码安卓版
  5. R语言禁止数值表示为科学计数法实战(Turn Off Scientific Notation ):全局设置或者单变量设置
  6. R语言对dataframe进行行数据筛选(row selection)多种方案:使用R原生方法、data.table、dplyr等方案
  7. R语言使用gganimate包和ggforce包可视化动画并动态缩放(zoom)移动动画的内容
  8. R语言str_starts函数和str_ends函数检查在字符串的开头或者结尾是否存在特定字符串或者字符串模式
  9. R语言使用lubridate包的tz函数设置和查询日期、时间对象的时区信息( time zone)
  10. FileNotFoundError: [Errno 2] No such file or directory: _MEI138162\\astor\\VERSION‘