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());

}

}

相关推荐
hqxstudying1 小时前
mybatis过渡到mybatis-plus过程中需要注意的地方
java·tomcat·mybatis
重生之我是一名前端程序员5 小时前
websocket + xterm 前端实现网页版终端
前端·websocket
你我约定有三6 小时前
RabbitMQ--消费端异常处理与 Spring Retry
spring·rabbitmq·java-rabbitmq
shuair8 小时前
07 - spring security基于数据库的账号密码
spring·spring security
Java水解8 小时前
深度剖析【Spring】事务:万字详解,彻底掌握传播机制与事务原理
后端·spring
杨杨杨大侠9 小时前
第3篇:配置管理的艺术 - 让框架更灵活
java·spring·log4j
Java码农田11 小时前
springmvc源码分析全体流程图
spring·源码
做一位快乐的码农12 小时前
房屋装修设计管理系统的设计与实现/房屋装修管理系统
java·struts·spring·eclipse·tomcat·maven
麦兜*14 小时前
【Prometheus】 + Grafana构建【Redis】智能监控告警体系
java·spring boot·redis·spring·spring cloud·grafana·prometheus
孟婆来包棒棒糖~20 小时前
Maven快速入门
java·spring boot·spring·maven·intellij-idea