簡體   English   中英

嵌入式tomcat 8.0.21中的Spring websocket

[英]Spring websocket in embedded tomcat 8.0.21

我想基於一個例子創建一個WebSocket。 唯一的轉折是我在嵌入式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):

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

從日志看起來spring-websocket似乎已成功初始化,但當我嘗試連接到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

我錯過了什么?

提前致謝!

也許我找到了解決問題的方法。

您需要將WsSci servlet容器初始化程序用於嵌入式tomcat上下文:

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

https://github.com/spring-projects/spring-boot/issues/439

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM