关键词:
websocket是全双工通信协议,目前html5支持,如果是app端的话可能不支持,建议app端实现通过tcp握手长连接实现通信,这里暂不研究。
首先websocket是一个协议,需要了解一下
第一步先引入starter
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-websocket</artifactId> 4 <version>${websocket.version}</version> 5 </dependency>
编写一个websocket终端类
1 @ServerEndpoint(value = "/websocket") 2 @Component 3 public class FinePetroWebSocket { 4 5 private static ApplicationContext applicationContext; 6 7 public static void setApplicationContext(ApplicationContext context) { 8 applicationContext = context; 9 } 10 11 /** 12 * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 13 */ 14 private static CopyOnWriteArraySet<FinePetroWebSocket> webSocketSet = new CopyOnWriteArraySet<>(); 15 16 /** 17 * 与某个客户端的连接会话,需要通过它来给客户端发送数据 18 */ 19 private Session session; 20 21 /** 22 * 存储session的map 23 */ 24 private static Map<String, Session> sessionMap = new ConcurrentHashMap<>(); 25 26 27 28 29 /** 30 * 连接建立成功调用的方法 31 */ 32 @OnOpen 33 public void onOpen(Session session) { 34 this.session = session; 35 //加入set中 36 webSocketSet.add(this); 37 38 39 } 40 41 /** 42 * 连接关闭调用的方法 43 */ 44 @OnClose 45 public void onClose() { 46 //从set中删除 47 webSocketSet.remove(this); 48 } 49 50 /** 51 * 收到客户端消息后调用的方法 52 * 53 * @param message 客户端发送过来的消息 54 */ 55 @OnMessage 56 public void onMessage(String message, Session session) { 57 58 59 } 60 61 private void sendMessage(Session session, String message) throws IOException { 62 session.getBasicRemote().sendText(message); 63 } 64 65 66 67 68 69 }
1 @Configuration 2 public class WebSocketConfig { 3 @Bean 4 public ServerEndpointExporter serverEndpointExporter() { 5 return new ServerEndpointExporter(); 6 } 7 8 }
这样就可以实现通讯了 , 但是注意这里的一个坑就是websocket每个链接使用一个对象,这里就不算是单例的,所以注入的service会为null,所以要使用static静态变量,在application中调用。
@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
springboot入门:集成websocket,实时显示系统日志
以前面的博客为基础,最近一篇为SpringBoot入门(十):集成Redis哨兵模式,实现Mybatis二级缓存。本篇博客主要介绍了SpringBoot集成WebSocket进行日志的推送,并实时显示在页面上。1.导入jar包第一个jar包是websocket的,第二个jar包是... 查看详情
将 Java WebSockets (JSR-356) 与 SpringBoot 集成
】将JavaWebSockets(JSR-356)与SpringBoot集成【英文标题】:IntegratingJavaWebSockets(JSR-356)withSpringBoot【发布时间】:2017-07-1618:33:00【问题描述】:在SpringBoot中部署websocket时遇到问题。我已经尝试了很多基于https://spring.io/blog/2013/05/23/spring-fra... 查看详情
项目总结48:springboot集成websocket案例(代码片段)
项目总结48:Springboot集成Websocket案例 Springboot集成Websocket的具体实现由很多方式,但原理是一样的;先放一个具体的案例 pom.xml jar依赖<!--websocket--><dependency><groupId>org.springframework.boot</groupId><artifactId... 查看详情
springboot——springboot集成websocket实现简单的多人聊天室(代码片段)
文章目录:1.什么是WebSocket?2.Java中的WebSocketAPI2.1WebSocket开发中的相关注解及API方法2.2前端技术对WebSocket的支持3.多人聊天室的实现源码3.1pom文件中添加相关依赖2.2在核心配置文件中配置视图解析器2.3加入相关静态资源文... 查看详情
springboot集成websocket,轻松实现信息推送!
在一次项目开发中,使用到了Netty网络应用框架,以及MQTT进行消息数据的收发,这其中需要后台来将获取到的消息主动推送给前端,于是就使用到了MQTT,特此记录一下。一、什么是websocket?WebSocket协议是基于TCP的一种新的网络... 查看详情
springboot+vue3集成使用websocket(代码片段)
...ocket</artifactId></dependency>增加配置类,声明该springboot项目使用websocket@ConfigurationpublicclassWebSocketConfig@BeanpublicServerEndpointExporterserverEndpointExporter()returnnewServerEndpointExporter();第三步,新建包增加业务代码:... 查看详情
springboot+vue3集成使用websocket(代码片段)
...ocket</artifactId></dependency>增加配置类,声明该springboot项目使用websocket@ConfigurationpublicclassWebSocketConfig@BeanpublicServerEndpointExporterserverEndpointExporter()returnnewServerEndpointExporter();第三步,新建包增加业务代码:... 查看详情
springboot集成websocket,实现后台向前端推送信息(代码片段)
前言在一次项目开发中,使用到了Netty网络应用框架,以及MQTT进行消息数据的收发,这其中需要后台来将获取到的消息主动推送给前端,于是就使用到了MQTT,特此记录一下。一、什么是websocket?WebSocket协... 查看详情
springboot集成websocket,实现后台向前端推送信息(代码片段)
前言在一次项目开发中,使用到了Netty网络应用框架,以及MQTT进行消息数据的收发,这其中需要后台来将获取到的消息主动推送给前端,于是就使用到了MQTT,特此记录一下。一、什么是websocket?WebSocket协... 查看详情
springboot2.1.1中集成websocket单元测试异常
单元测试在没有集成websocket之前是好好的,当集成websocket之后就出现了下面的异常(只贴出来关键信息):2019-01-1110:05:42[ERROR][org.springframework.boot.SpringApplication:858]-ApplicationrunfailedCausedby:org.springframework.beans.factory.Bean 查看详情
springboot使用websocket(代码片段)
...这句话,不是我超脱了,是有时我们醒悟了;注意标题:springboot使用websocket 1】第一步:引入依赖: <!--集成w 查看详情
springboot深入学习-----tomcat配置websocket
一、更改servlet服务器springboot中默认可以集成多种servlet容器,当引入如下依赖时:springboot默认以tomcat作为项目的servlet容器,如果用户想要替换tomcat为jetty或者undertow,只需要做以下更改: 二、tomcat配置 目前来说tomcat... 查看详情
springboot集成websocket,实现后台向前端推送信息(代码片段)
...麻烦了。于是websocket应运而生。下面我们就直接开始使用Springboot开始整合。以下案例都在我自己的电脑上测试成功,你可以根据自己的功能进行修改即可。我的项目结构如下:二、使用步骤1.添加依赖Maven依赖: <d... 查看详情
大屏的接口开发一:基于springboot的websocket数据推送(代码片段)
...的免手动刷新,实现数据的同步变动展示。这里主要使用SpringBoot集成webSocket实现数据的全局发送,指定用户发送,值得注意的是这里使用了Spring自带的握手拦截器获取前端传来的userId,与webSocket的会话进行绑定,用于长连接的... 查看详情
大屏的接口开发一:基于springboot的websocket数据推送(代码片段)
...的免手动刷新,实现数据的同步变动展示。这里主要使用SpringBoot集成webSocket实现数据的全局发送,指定用户发送,值得注意的是这里使用了Spring自带的握手拦截器获取前端传来的userId,与webSocket的会话进行绑定,用于长连接的... 查看详情
springboot集成netty-socket.io
参考技术Anetty-socekt.IO官网socket.io是一个netty.socketnode版的java实现版,其性能优于webSocket等socket技术,socket.io有nameSpace等,分区方式,比较灵活。originHost为socket客户端的地址,serverHost请使用ip,lz在使用过程中尝试过使用localhost,... 查看详情
websocket客户端/服务端代码(代码片段)
服务端服务端我们采用SpringBoot方式集成WebSocket。关键代码如下:开启WebSocket功能:@ConfigurationpublicclassWebSocketConfig@BeanpublicServerEndpointExporterserverEndpointExporter()returnnewServerEndpointExporter();如果 查看详情
springboot使用websocket
目录springboot使用WebSocket前端:后端springboot使用WebSocket来源:https://blog.lqdev.cn/2018/08/14/springboot/chapter-nineteen/类似聊天室的功能,WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议。在WebSocketAPI中,浏览器和服务器只... 查看详情