gpt4 book ai didi

java - 使用 spring 启用 WebSocket

转载 作者:行者123 更新时间:2023-11-30 07:12:19 35 4
gpt4 key购买 nike

我正在尝试使用 Wildfly 10 服务器使用 Spring 配置 Web 套接字。根据this教程,我有以下文件:

这是网络套接字类:

package com.myapp.spring.web.controller;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;

import org.springframework.web.socket.server.standard.SpringConfigurator;


@ServerEndpoint(value="/serverendpoint", configurator = SpringConfigurator.class)

/**
* This class creates web sockets, opens, and maintains connection with the client
*/
public class serverendpoint {


@OnOpen
public void handleOpen () {
System.out.println("JAVA: Client is now connected...");
}

@OnMessage
public String handleMessage (String message) {

if (message.equals("ping"))
return "pong";
else if (message.equals("close")) {
handleClose();
return null;
}
System.out.println("JAVA: Received from client: "+ message);
if (message.contains("//")) {
MyClass mc = new MyClass(message);
return mc.someMethod();
} else {
System.out.println("Message From Web Socket Not Understood");
return null;
}
}

@OnClose
public void handleClose() {
System.out.println("JAVA: Client is now disconnected...");
}

@OnError
public void handleError (Throwable t) {
t.printStackTrace();
}
}

这是网络套接字配置文件:

package com.myapp.spring.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import com.myapp.spring.web.controller.serverendpoint;

@Configuration
public class EndpointConfig {

@Bean
public serverendpoint serverendpoint() {
return new serverendpoint();
}

@Bean
public ServerEndpointExporter endpointExporter() {
return new ServerEndpointExporter();
}

}

这是我的pom.xml:

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>

根据教程,这就是我所要做的。但我收到以下错误:

Failed to start service jboss.undertow.deployment.default-server.default-host./ROOT: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./ROOT: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:231)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
... 6 more
Caused by: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
at io.undertow.websockets.jsr.Bootstrap$WebSocketListener.contextInitialized(Bootstrap.java:104)
at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)
... 8 more

这个问题有什么解决办法吗?此外,是否需要添加任何其他配置文件,以便将我的 Web 套接字正确映射到端点 /serverendpoint,就像我在 serverendpoint() 中所做的那样类(我问这个是因为我有点不确定我是否只需要一个配置文件。这似乎不对。我环顾四周, others 包含了其他文件,例如 @ EnableWebSocket,但是教程说我只需要这两个文件。)?

非常感谢!

最佳答案

请通过https://github.com/spring-projects/spring-boot/issues/6166看看这是否能解决您的问题。 SO 中报告了类似的问题 Spring Boot Websockets in Wildfly 。希望这会有所帮助。

关于java - 使用 spring 启用 WebSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39028763/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com