博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot Websocket
阅读量:2439 次
发布时间:2019-05-10

本文共 3927 字,大约阅读时间需要 13 分钟。

在springboot项目中使用websocket做推送,虽然挺简单的,但初学也踩过几个坑,特此记录。

使用websocket有两种方式:1. 是使用sockjs,2. 是使用h5的标准。使用Html5标准自然更方便简单,所以记录的是配合h5的使用方法。

1、pom

核心是@ServerEndpoint这个注解。这个注解是Javaee标准里的注解,tomcat7以上已经对其进行了实现,如果是用传统方法使用tomcat发布项目,只要在pom文件中引入javaee标准即可使用。

javax
javaee-api
7.0
provided

但使用springboot的内置tomcat时,就不需要引入javaee-api了,spring-boot已经包含了。使用springboot的websocket功能首先引入springboot组件。

org.springframework.boot
spring-boot-starter-websocket
1.3.5.RELEASE

顺便说一句,springboot的高级组件会自动引用基础的组件,像spring-boot-starter-websocket就引入了spring-boot-starter-web和spring-boot-starter,所以不要重复引入。

2、使用@ServerEndpoint创立websocket endpoint

首先要注入ServerEndpointExporter,这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint。要注意,如果使用独立的servlet容器,而不是直接使用springboot的内置容器,就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。

@Configurationpublic class WebSocketConfig {
@Bean public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter(); }}

接下来就是写websocket的具体实现类,很简单,直接上代码:

@ServerEndpoint(value = "/websocket")@Componentpublic class MyWebSocket {
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 private static int onlineCount = 0; //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 private static CopyOnWriteArraySet
webSocketSet = new CopyOnWriteArraySet
(); //与某个客户端的连接会话,需要通过它来给客户端发送数据 private Session session; /** * 连接建立成功调用的方法*/ @OnOpen public void onOpen(Session session) {
this.session = session; webSocketSet.add(this); //加入set中 addOnlineCount(); //在线数加1 System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); try {
sendMessage(CommonConstant.CURRENT_WANGING_NUMBER.toString()); } catch (IOException e) {
System.out.println("IO异常"); } } /** * 连接关闭调用的方法 */ @OnClose public void onClose() {
webSocketSet.remove(this); //从set中删除 subOnlineCount(); //在线数减1 System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); } /** * 收到客户端消息后调用的方法 * * @param message 客户端发送过来的消息*/ @OnMessage public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message); //群发消息 for (MyWebSocket item : webSocketSet) {
try {
item.sendMessage(message); } catch (IOException e) {
e.printStackTrace(); } } } /** * 发生错误时调用 */ @OnError public void onError(Session session, Throwable error) {
System.out.println("发生错误"); error.printStackTrace(); } public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message); //this.session.getAsyncRemote().sendText(message); } /** * 群发自定义消息 * */ public static void sendInfo(String message) throws IOException {
for (MyWebSocket item : webSocketSet) {
try {
item.sendMessage(message); } catch (IOException e) {
continue; } } } public static synchronized int getOnlineCount() {
return onlineCount; } public static synchronized void addOnlineCount() {
MyWebSocket.onlineCount++; } public static synchronized void subOnlineCount() {
MyWebSocket.onlineCount--; }}

使用springboot的唯一区别是要@Component声明下,而使用独立容器是由容器自己管理websocket的,但在springboot中连容器都是spring管理的。

虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。

3、前端代码

    My WebSocketWelcome

4、总结

springboot已经做了深度的集成和优化,要注意是否添加了不需要的依赖、配置或声明。由于很多讲解组件使用的文章是和spring集成的,会有一些配置,在使用springboot时,由于springboot已经有了自己的配置,再这些配置有可能导致各种各样的异常。

转载地址:http://bggmb.baihongyu.com/

你可能感兴趣的文章
对移动通信网络优化工作的一些见解(转)
查看>>
正确网络配置建议 减少卡机死机的关键(转)
查看>>
智能手机Smartphone开发从零起步(五)(转)
查看>>
SEO技巧中你可能没有注意的细节(转)
查看>>
微软开始二代Windows Live 不见Cloud OS踪影
查看>>
创建ISAPI扩展(转)
查看>>
病毒及木马预警一周播报(06.04.17~04.23)(转)
查看>>
黑客口述:我的第一台3389肉鸡的经历(转)
查看>>
关于 cleanup stack 和 two phase consturction [1](转)
查看>>
Oracle数据导入导出imp/exp (转)
查看>>
如何构建固定网(PSTN)短消息系统(转)
查看>>
Delphi文件管理(三)(转)
查看>>
关于网线的一些问题的解答(转)
查看>>
深度分析Win 2003自动升级补丁功能(转)
查看>>
使用Carbide.vs与VS.NET2003构建Symbian开发平台-S60 平台(转)
查看>>
来访者地址统计,很好的一个程序!(转)
查看>>
UpdateWindow函数 (转)
查看>>
无盘网络正确网络配置建议-减少卡机蓝屏关键(转)
查看>>
如何在Delphi中调用oracle的存储过程返回数据集(转)
查看>>
ASP指南:ADO/SQL(数据存取) (转)
查看>>