使用 spring 启用 WebSockets
Posted
技术标签:
【中文标题】使用 spring 启用 WebSockets【英文标题】:Enabling WebSockets using spring 【发布时间】:2016-12-26 00:11:39 【问题描述】:我正在尝试使用 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
,但教程说我只需要这两个文件。) ?
非常感谢!
【问题讨论】:
【参考方案1】:请通过https://github.com/spring-projects/spring-boot/issues/6166 看看这是否能解决您的问题。 在Spring Boot Websockets in Wildfly 的 SO 中报告了一个类似的问题。希望这会有所帮助。
【讨论】:
我会调查的,非常感谢。但是除了配置文件,我还需要其他文件让我的 web socket 在 spring 上工作吗? 您正在学习的教程有一个示例项目,地址为github.com/rstoyanchev/spring-websocket-test。请看看这是否对您有帮助。 现在,我收到一个新错误: 原因:javax.websocket.DeploymentException: UT003023: 具有相同逻辑映射的多个端点 PathTemplatetemplate=false, base='/serverendpoint', parts=[ ] 和 PathTemplatetemplate=false, base='/serverendpoint', parts=[] 你改变了什么吗?尝试 maven clean package,从 jboss 取消部署旧版本并重新启动服务器。 不,虽然我在 pom.xml 中添加了一个 undertow 依赖项以上是关于使用 spring 启用 WebSockets的主要内容,如果未能解决你的问题,请参考以下文章
如何解决在使用 Spring Boot 和 Spring Security 启用 CSRF 后无法正常工作的登录问题?
如何使用 Spring MVC 和 Spring Security 为资源处理程序启用 HTTP 缓存