Spring boot实现原生websocket
网上的大部分教程是基于sockjs,这篇文章内容则是基于原生协议。
后台Spring boot 配置
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Resource
private WsSiteHandler wsSiteHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(wsSiteHandler, "site/change").setAllowedOrigins("*");
}
}
@Component
public class WsSiteHandler extends TextWebSocketHandler {
private final static Logger logger = LoggerFactory.getLogger(WsSiteHandler.class);
private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
private ExecutorService executor = Executors.newCachedThreadPool();
public void asyncNotice(SiteTo to) {
executor.execute(() -> {
String json = new Gson().toJson(to);
handleTextMessage(null, new TextMessage(json));
});
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
if (session == null) {
sessions.removeIf(el -> !el.isOpen());
sessions.forEach(el -> {
try {
if (el.isOpen()) {
el.sendMessage(message);
} else {
el.close();
}
} catch (IOException e) {
logger.error("发送车位信息错误!", message.toString(), e);
}
});
} else {
try {
session.sendMessage(message);
} catch (IOException e) {
logger.error("发送车位信息错误!", message.toString(), e);
}
}
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
sessions.add(session);
}
}
前台调用
initWs () {
if (!window.WebSocket) return
this.socket = new WebSocket(`ws://127.0.0.1:9000/marketing/site/change`)
this.socket.onopen = (event) => {
console.log('onopen', event)
}
this.socket.onmessage = (event) => {
console.log('onmessage', event.data)
const siteTo = JSON.parse(event.data)
siteTo.lock = siteTo.isLock
const index = this.result.findIndex(el => el.site.id === siteTo.site.id)
if (index >= 0) {
this.result.splice(index, 1, siteTo)
}
}
this.socket.onerror = (event) => {
console.log('onerror', event)
}
}
Spring boot实现原生websocket的更多相关文章
- 在spring boot中使用webSocket组件(二)
该篇演示如何使用websocket创建一对一的聊天室,废话不多说,我们马上开始! 一.首先先创建前端页面,代码如下图所示: 1.login.html <!DOCTYPE html> < ...
- 在spring boot中使用webSocket组件(一)
最近在项目中使用到了spring的webSocket组件,在这里和大家分享下,如有错误,欢迎大家指正. 在这里我使用的IDE工具是Intellij idea,框架是spring boot.spring ...
- Spring Boot 开发集成 WebSocket,实现私有即时通信系统
1/ 概述 利用Spring Boot作为基础框架,Spring Security作为安全框架,WebSocket作为通信框架,实现点对点聊天和群聊天. 2/ 所需依赖 Spring Boot 版本 ...
- Spring Boot中使用Websocket搭建即时聊天系统
1.首先在pom文件中引入Webscoekt的依赖 <!-- websocket依赖 --> <dependency> <groupId>org.springfra ...
- Spring Boot 中使用 WebSocket 实现一对多聊天及一对一聊天
为什么需要WebSocket? 我们已经有了http协议,为什么还需要另外一个协议?有什么好处? 比如我想得到价格变化,只能是客户端想服务端发起请求,服务器返回结果,HTTP协议做不到服务器主动向客户 ...
- Spring Boot 整合单机websocket(附github源码)
websocket 概念 websocket 是一个通信协议,通过单个 TCP 连接提供全双工通信.websocket 连接成功后,服务端和客户可以进行双向通信.不同于 http 通信协议需要每次由客 ...
- 【websocket】spring boot 集成 websocket 的四种方式
集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...
- 在Spring Boot框架下使用WebSocket实现消息推送
Spring Boot的学习持续进行中.前面两篇博客我们介绍了如何使用Spring Boot容器搭建Web项目(使用Spring Boot开发Web项目)以及怎样为我们的Project添加HTTPS的 ...
- Spring Boot 系列 - WebSocket 简单使用
在实现消息推送的项目中往往需要WebSocket,以下简单讲解在Spring boot 中使用 WebSocket. 1.pom.xml 中引入 spring-boot-starter-websock ...
随机推荐
- 说说PC站和移动站的移动适配关系优化
曾经写过关于手机网站的SEO优化方向,但是多数是注重在移动网站代码方面,而把移动和PC的重点关系优化给忽略了,这方面也是很多做SEO优化站长给忽略的一些事情. 2015年11月6日,在百度站长平台可以 ...
- SpringBoot文件上传(MVC情况和webFlux情况)
MVC情况 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- JSP三大指令 六大内置对象
(1)include指令 作用: 在当前页面用于包含其他页面 语法: <%@include file=”common/header.jsp”%> (2)page指令 作用: 告诉tomca ...
- elasticsearch-head的安装
elasticsearch-head是es的一个可视化的客户端插件,可以直接对ES进行增删改查操作,安装前需要先安装NODEJS: 安装: 1.到git上下载源代码: # git clone git: ...
- CNN 激活函数
CNN: 1\ Siamoid 2\ Relu + Softplus 图片来源: http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeu ...
- Vue学习笔记二:v-cloak,v-text,v-html的使用
目录 v-cloak:解决插值表达式闪烁问题 安装插件Live Server 右键以HTTP形式运行HTML v-text:以属性方式使用插值表达式 v-cloak和v-text的区别 v-html: ...
- 使用numpy的小惊喜
今天使用 numpy.true_divide 发现个有趣的事情, 下面的代码18.19行如果去掉,就会报下面的 RuntimeWarning def multivalue_divide(timese ...
- [译]Ocelot - Service Discovery
原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...
- FPGA
FPGA(Field-Programmable Gate Array),即现场可编程门阵列,它是在PAL.GAL.CPLD等可编程器件的基础上进一步发展的产物.它是作为专用集成电路(ASIC)领域中的 ...
- 关于MarkDown里的图片问题
网上看了很多人为了得到那串URL,需要弄什么python,还有自己弄个服务器. 在我看来这些都是多此一举,只要有个GitHub,然后再开两个页面,一个页面写readme,另一个写issue(不是真的写 ...