嵌入式 tomcat 8.0.21 中的 Spring websocket

编程入门 行业动态 更新时间:2024-10-25 08:27:38
本文介绍了嵌入式 tomcat 8.0.21 中的 Spring websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想基于示例创建一个 WebSocket.唯一的问题是我在嵌入式 tomcat 中运行我的应用程序.

I want to create a WebSocket based on an example. The only spin is that I'm running my application in embedded tomcat.

package com.test.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

public class WebSocketTest extends AbstractWebSocketHandler {
    private static Logger logger = LoggerFactory.getLogger(WebSocketTest.class);

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        logger.debug("Recieved websocket message: " + message);

        session.sendMessage(new TextMessage("thanks"));
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        logger.info("WebSocket connection established!");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        logger.info("WebSocket connection closed!");
    }
}

配置(websocket.spring.xml):

Configuration(websocket.spring.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
       xmlns:websocket="http://www.springframework/schema/websocket"
       xsi:schemaLocation="http://www.springframework/schema/beans
              http://www.springframework/schema/beans/spring-beans.xsd
              http://www.springframework/schema/websocket
              http://www.springframework/schema/websocket/spring-websocket.xsd">

       <websocket:handlers>
              <websocket:mapping path="/socket" handler="webSocketHandler"/>
       </websocket:handlers>

       <bean id="webSocketHandler" class="com.test.websocket.WebSocketTest"/>
</beans>

Servlet 映射位于/websocket/*

Servlet mapping is at /websocket/*

从日志看来,spring-websocket 似乎已成功初始化,但是当我尝试连接到 websocket 时出现此错误.

From the log It seems like the spring-websocket is initialized successfully however I'm getting this error, when I try to connect to the websocket.

Servlet.service() for servlet [spring-websocket] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:9876/websocket/socket; nested exception is java.lang.IllegalArgumentException: No 'javax.websocket.server.ServerContainer' ServletContext attribute. Are you running in a Servlet container that supports JSR-356?] with root cause

依赖项是:tomcat-embed-core、tomcat-embed-websocket、tomcat-juli 并且因为 (Spring websocket 示例 - 错误 - 您是否在支持 J SR-356 的 Servlet 容器中运行?)javax.websocket-api 和tomcat-websocket

Dependencies are: tomcat-embed-core, tomcat-embed-websocket, tomcat-juli and because of (Spring websocket example - error - Are you running in a Servlet container that supports J SR-356?) javax.websocket-api and tomcat-websocket

我错过了什么?

提前致谢!

推荐答案

也许我找到了解决您问题的方法.

Maybe i found a solution to your problem.

您需要使用 WsSci servlet 容器初始化器来嵌入 tomcat 上下文:

You need to use WsSci servlet container initializer to the embedded tomcat context:

context.addServletContainerInitializer(new WsSci(), null);

context.addServletContainerInitializer(new WsSci(), null);

https://github/spring-projects/spring-boot/问题/439

这篇关于嵌入式 tomcat 8.0.21 中的 Spring websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 04:08:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌入式   tomcat   websocket   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!