spring-websocket 和 tomcat-embed-websocket 有什么区别,如何使用

`spring-websocket` 和 `tomcat-embed-websocket` 都是用于支持 WebSocket 协议的库,但它们有不同的用途和实现方式。

  1. **Spring WebSocket (`spring-websocket`)**:
  • `spring-websocket` 是 Spring Framework 的一部分,它提供了一种在 Spring 应用程序中处理 WebSocket 连接的方式。

  • 它允许您在 Spring 应用程序中轻松地创建 WebSocket 端点,并通过 Spring MVC 控制器或消息代理处理来自客户端的消息。

  • Spring WebSocket 提供了高级功能,如消息拦截器、消息转换器、心跳检测等,使得开发 WebSocket 应用程序更加灵活和方便。

  • Spring WebSocket 可以与 Spring 的其他模块(如 Spring Security、Spring Boot)无缝集成,提供全面的解决方案。

  1. **Tomcat Embed WebSocket (`tomcat-embed-websocket`)**:
  • `tomcat-embed-websocket` 是 Apache Tomcat 的一部分,它提供了 Tomcat 内置的 WebSocket 实现。

  • 它允许您在 Tomcat 服务器中创建和管理 WebSocket 端点,并直接使用 Tomcat 提供的 API 来处理 WebSocket 连接。

  • Tomcat Embed WebSocket 通常用于基于 Tomcat 的应用程序,或者通过嵌入式 Tomcat 启动的 Spring Boot 应用程序。

总的来说,`spring-websocket` 更适合于使用 Spring 框架开发的应用程序,它提供了更丰富的功能和更好的集成性。而 `tomcat-embed-websocket` 则更适合于直接在 Tomcat 服务器中开发 WebSocket 应用程序,或者与 Tomcat 集成的场景。

代码示例:

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import org.springframework.web.socket.CloseStatus;

import org.springframework.web.socket.TextMessage;

import org.springframework.web.socket.WebSocketSession;

import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component

public class WebSocketHandler extends TextWebSocketHandler {

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Override

public void afterConnectionEstablished(WebSocketSession session) throws Exception {

// 在线人数加1

WebSocketUtil.countAtomic.incrementAndGet();

String userId = session.getAttributes().get("userId").toString();

logger.info("用户:" + userId + "已经上线");

WebSocketUtil.clients.put(userId, session);

logger.info("连接打开, 当前连接数:{},当前在线人数:{}", WebSocketUtil.countAtomic.get(), WebSocketUtil.clients.size());

}

@Override

public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {

logger.info("收到客户端的消息:" + message.getPayload());

}

@Override

public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {

// 在线人数减一

WebSocketUtil.countAtomic.decrementAndGet();

String userId = session.getAttributes().get("userId").toString();

WebSocketUtil.clients.remove(userId);

logger.info("连接关闭, 当前连接数:{},当前在线人数:{}", WebSocketUtil.countAtomic.get(), WebSocketUtil.clients.size());

}

@Override

public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {

logger.info("服务端发生了错误" + exception.getMessage());

}

}

相关推荐
大G的笔记本25 分钟前
Spring IOC和AOP
java·后端·spring
hello 早上好2 小时前
解析 Prompt 工程中的四大核心角色
spring
兮动人5 小时前
Spring中@Configuration注解的proxyBeanMethods属性详解
java·后端·spring
Jing_jing_X7 小时前
Spring 自动注入是怎么实现的?从 @Component 到 @Autowired 的完整流程
java·后端·spring
知其然亦知其所以然8 小时前
我被问懵了:Tomcat 到底有几种部署方式?
后端·面试·tomcat
李昊哲小课8 小时前
spring 中 HttpStatus 与 ResponseEntity
spring boot·后端·spring·http·spring cloud·restful
or77iu_N9 小时前
Linux 解压安装(安装tomcat)
linux·运维·tomcat
后端小张10 小时前
【JAVA 进阶】SpringBoot集成Sa-Token权限校验框架深度解析
java·spring boot·spring·架构·sa-token·springboot·权限框架
DokiDoki之父11 小时前
SpringMVC—请求映射路径 & get请求与Post请求发送请求参数 & 5种类型参数传递 & json数据传递参数 & 日期型参数的传递 & 响应
spring
weixin_4196583111 小时前
Spring的三级缓存和SpringMVC的流程
java·spring·缓存