SpringBoot集成WebSocket的两种方式
websocketadmin 发布于:2023-06-10 20:55:18
阅读:loading
本站在2014年4月时曾全面的学习HTML5的技术,特写过HTML5的WebSocket示例,当时使用的Servlet3.0规范中的API,需要Tomcat7的支持(貌似在Tomcat6的后期维护版本也增加了WebSocket的支持),早在当初该示例还是本站的一个特色功能,好多来找我要源码的呢。时隔多年再来使用SpringBoot架构来体验一下集成WebSocket的实现,经过一番资料的百科大概有找到使用两种方式的实现,我分别对它们进行了实践,所以我称这两种方式为JDK内置版和Spring封装版。
主要是使用javax.websocket包下的注解进行集成,主要有:ServerEndpoint、OnOpen、OnMessage、OnClose、OnError相关的类和注解来集成,整体上比较简单,都是基于注解定义的方法来声明的,参考如下代码所示:
package cn.chendd.websocket.jdk;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
/**
* websocket实现
*
* @date 2023/6/2 21:02
*/
@Component
@ServerEndpoint(value = "/websocket/jdk")
public class JdkWebSocket {
@OnOpen
public void onOpen(Session session) {
System.out.println("WebSocketConfig.onOpen");
}
@OnMessage
public void onMessage(Session session , String message) {
System.out.println("WebSocketConfig.onMessage-->" + session + "--->" + message);
}
@OnClose
public void onClose() {
System.out.println("WebSocketConfig.onClose");
}
@OnError
public void onError(Session sesison , Throwable throwable) {
System.out.println("WebSocketConfig.onError-->" + sesison + "--->" + throwable);
}
}
package cn.chendd.websocket.jdk;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author chendd
* @date 2023/6/2 21:15
*/
@Configuration
public class JdkWebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
(ws://127.0.0.1:8080/websocket/jdk)
Spring封装版本有对于消息类型进行封装,提供了更加全面的WebSocket方面的API,值得深入分析。
package cn.chendd.websocket.spring;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;
/**
* SpringWebSocketHandler
*
* @author chendd
* @date 2023/6/2 22:08
*/
@Component
public class SpringWebSocketHandler extends TextWebSocketHandler {
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
super.afterConnectionEstablished(session);
System.out.println("SpringWebSocketHandler.afterConnectionEstablished");
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
super.handleMessage(session, message);
System.out.println("SpringWebSocketHandler.handleMessage");
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session, message);
System.out.println("SpringWebSocketHandler.handleTextMessage");
}
@Override
protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
super.handlePongMessage(session, message);
System.out.println("SpringWebSocketHandler.handlePongMessage");
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
super.handleTransportError(session, exception);
System.out.println("SpringWebSocketHandler.handleTransportError");
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
super.afterConnectionClosed(session, status);
System.out.println("SpringWebSocketHandler.afterConnectionClosed");
}
@Override
public boolean supportsPartialMessages() {
System.out.println("SpringWebSocketHandler.supportsPartialMessages");
return super.supportsPartialMessages();
}
}
package cn.chendd.websocket.spring;
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;
import javax.annotation.Resource;
/**
* SpringWebSocketConfig
*
* @author chendd
* @date 2023/6/2 22:11
*/
@Configuration
@EnableWebSocket
public class SpringWebSocketConfig implements WebSocketConfigurer {
@Resource
private SpringWebSocketHandler springWebSocketHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(springWebSocketHandler , "/websocket/spring").setAllowedOrigins("*");
}
}
(ws://127.0.0.1:8080/websocket/spring)
(1)本次示例重点在于服务端Java代码的示例,未编写前端示例,可在下方的项目源码中参见前端的HTML集成,或者是使用本例中使用的在线WebSocket测试的页面地址进行在线验证,右键查看其源代码也是原生的HTML5规范的相关代码;
(2)提供了JDK的内置版本和Spring的封装版本,个人推荐使用Spring的封装版,毕竟JDK的注解不够清晰的描述出被注解的方法的方法具体参数,而Spring封装版本有对于消息类型进行封装;
(3)代码比较简单,结合前文的《一个猜你所选的小程序随写》中的全量代码也包含在内,代码包结构如下:
(4)完整源码可见:《SpringBoot集成WebSocket项目源码.txt》;
点赞