Springboot Websocket + STOMP + SOCKJS无法与JBOSS EAP 6.4一起使用并抛出404 Not found

问题描述

问题:无法访问JBOSS EAP 6.4服务器中的Springboot Websocket端点。使用SOCKJS访问websocket时,在浏览器控制台中出现404错误

有关问题和步骤的更多详细信息:

我使用了这个项目Spring offical reference。这份春季参考资料使用webjars软件包作为客户端Web库。但是它没有加载客户端JS库,因此我修改了如下所示的链接以使其正常工作。

<link href="https://cdn.jsdelivr.net/webjars/org.webjars/bootstrap/3.4.1/bootstrap.min.css" rel="stylesheet">
<link href="/main.css" rel="stylesheet">
<script src="/app.js"></script>
<script src="https://cdn.jsdelivr.net/webjars/org.webjars/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/webjars/org.webjars/sockjs-client/1.1.2/sockjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/webstomp-client@1.2.6/dist/webstomp.min.js"></script>

也尝试过JBOSS VFS库,但是没有加载webjar的运气(不确定这可能会引起问题)。我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>messaging-stomp-websocket</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>messaging-stomp-websocket</name>
    <description>Demo project for Spring Boot</description>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-websocket</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator-jboss-vfs</artifactId>
            <version>0.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>sockjs-client</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>stomp-websocket</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.1.1-1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <finalName>stomp</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>2.1.0.Beta1</version>
            </plugin>
        </plugins>
    </build>
</project>

技术

Springboot / 2.3.2.RELEASE Jboss EAP服务器/6.4 JDK / 1.8 Maven / 3.2

STS中运行项目时,我们能够成功看到websocket调用

[

Works fine in STS3

我构建了一个战争文件,并毫无问题地部署在JBOSS EAP 6.4服务器中,当我通过浏览器访问时,我们得到了

获取http:// localhost:8080 / gs-guide-websocket / info?t = 1601151988789 404(未找到)

JBOSS EAP 6.4 Issue

客户端Sockjs调用方法

function connect() {
    var socket = new SockJS('http://localhost:8080/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({},function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings',function (greeting) {
            showGreeting(JSON.parse(greeting.body).content);
        });
    });
}

服务器端代码

WebSocketConfig

@Configuration
@EnableWebSocketMessagebroker
public class WebSocketConfig implements WebSocketMessagebrokerConfigurer {

    @Override
    public void configureMessagebroker(MessagebrokerRegistry config) {
        config.enableSimplebroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
    }

}

控制器

@Controller
public class GreetingController {

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello," + HtmlUtils.htmlEscape(message.getName()) + "!");
    }
}

我们根据JBOSS EAP 6.4 offical reference在Jboss EAP 6.4中进行了以下更改。

  1. 在JBoss EAP服务器配置文件(standalone.xml)的Web子系统中启用了NIO2连接器
  2. 在jboss-web.xml文件中使用true启用了websockets

我们有认的Jboss上下文路径。

请帮助我们解决问题:

获取http:// localhost:8080 / gs-guide-websocket / info?t = 1601151988789 404(未找到)

此外,我们在STS控制台日志中看到以下信息,但在JBOSS EAP 6.4日志中没有此类信息。

[Messagebroker-3] oswscWebSocketMessagebrokerStats:WebSocketSession [1当前WS(1)-HttpStream(0)-HttpPoll(0),总计1,异常关闭0(连接失败,发送限制0,传输0错误)],stompSubProtocol [已处理的CONNECT(1)-CONNECTED(1)-disCONNECT(0)],stompbrokerRelay [null],inboundChannel [池大小= 6,活动线程= 0,排队的任务= 0,已完成的任务= 6] ,outboundChannel [池大小= 1,活动线程= 0,排队的任务= 0,已完成的任务= 1],sockJsScheduler [池大小= 8,活动线程= 1,排队的任务= 2,已完成的任务= 7]

感谢您的帮助!

也参考了以下解决方案,但没有运气。

Stackoverflow Link 1 Stackoverflow Link 2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)