在本文中,我们将为您详细介绍SpringWebSockets@SendTo映射中的路径变量的相关知识,并且为您解答关于spring路径映射注解的疑问,此外,我们还会提供一些关于#翻译#Spring4.
在本文中,我们将为您详细介绍Spring WebSockets @SendTo映射中的路径变量的相关知识,并且为您解答关于spring路径映射注解的疑问,此外,我们还会提供一些关于#翻译# Spring 4.0 中的 WebSocket 架构、Error creating bean with name ''webSocketServer''(springboot 整合 webstock)、HTML5 websockets与PHP websockets vs node.js websockets?、io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket...的有用信息。
本文目录一览:- Spring WebSockets @SendTo映射中的路径变量(spring路径映射注解)
- #翻译# Spring 4.0 中的 WebSocket 架构
- Error creating bean with name ''webSocketServer''(springboot 整合 webstock)
- HTML5 websockets与PHP websockets vs node.js websockets?
- io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket...
Spring WebSockets @SendTo映射中的路径变量(spring路径映射注解)
我有一个非常简单的Spring WebSocket应用程序。但是,我正在尝试使用路径变量进行订阅以及消息映射。
我在下面发布了一个释义的示例。我希望@SendTo
注解会根据订阅者的身份返回给订阅者fleetId
。即,POSTto /fleet/MyFleet/driver/MyDriver
应通知的订阅者/fleet/MyFleet
,但我没有看到此行为。
值得注意的是,订阅文字/fleet/{fleetId}
作品。这是故意的吗?我是否缺少某些配置?还是这不是它的工作方式?
我对WebSockets或这个Spring项目还不太熟悉,所以在此先感谢。
Controller.java
...@MessageMapping("/fleet/{fleetId}/driver/{driverId}")@SendTo("/topic/fleet/{fleetId}")public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { return new Simple(fleetId, driverId);}...
WebSocketConfig.java
@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/live"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/fleet").withSockJS(); }}
index.html
var socket = new SockJS(''/fleet'');var stompClient = Stomp.over(socket);stompClient.connect({}, function(frame) { // Doesn''t Work stompClient.subscribe(''/topic/fleet/MyFleet'', function(greeting) { // Works stompClient.subscribe(''/topic/fleet/{fleetId}'', function(greeting) { // Do some stuff });});
发送样品
stompClient.send("/live/fleet/MyFleet/driver/MyDriver", {}, JSON.stringify({ // Some simple content }));
答案1
小编典典即使@MessageMapping
支持占位符,它们也不会在@SendTo
目标位置公开/解析。当前,无法使用@SendTo
注释定义动态目的地。你可以SimpMessagingTemplate
暂时使用(无论如何这都是内部工作的方式)。这是你的操作方式:
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")public void simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { simpMessagingTemplate.convertAndSend("/topic/fleet/" + fleetId, new Simple(fleetId, driverId));}
在你的代码中,将目标’ / topic / fleet / {fleetId} ‘视为文字,这就是订阅它起作用的原因,只是因为你要发送到完全相同的目标。
如果你只想发送一些特定于用户的初始数据,则可以直接在订阅中返回:
@SubscribeMapping("/fleet/{fleetId}/driver/{driverId}")public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { return new Simple(fleetId, driverId);}
更新: 在Spring 4.2中,支持目标变量占位符,现在可以执行以下操作:
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")@SendTo("/topic/fleet/{fleetId}")public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { return new Simple(fleetId, driverId);}
#翻译# Spring 4.0 中的 WebSocket 架构
两年前,客户端与服务器端的全双工双向通信作为一个很重要的功能被纳入到WebSocket RFC 6455协议中。在HTML5中,WebSocket已经成为一个流行词,大家对这个功能赋予很多构想,很多时候甚至是不切实际的期望。在这篇文章中,我们将重点介绍下如何通过Spring Framework 4.0来构建一个基于 STMOP协议的WebSocket形式的应用。该应用通过 Message Broker向用户广播消息,并使用SockJS作为浏览器前端通信代码库。
Error creating bean with name ''webSocketServer''(springboot 整合 webstock)
今天踩了一个坑记录下,springboot 项目,整合 webstock, 总是报以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''webSocketServer'': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class
...
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.safesys.monitoring.util.WebSocketServer] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@2f6f3352]
...
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:248)
...
Caused by: java.lang.ClassNotFoundException: javax.websocket.Session
...
Disconnected from the target VM, address: ''127.0.0.1:50767'', transport: ''socket''
刚刚开始,各种找错,发现只要添加 ServerEndpointExporter 就报错了
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
也找过很多帖子,让你注释启动test类的@RunWith(SpringRunner.class) 或者修改@SpringBootTest为测试环境之类的
反正各种找,准备放弃的时候,我尝试看看 tomcate 自身问题,因为要打 war,之前 pom 做过修改
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--声明spring boot内嵌tomcat的作用范围 在运行时不起作用-->
<scope>provided</scope>
</dependency>
看了注释,我注释了<scope>provided</scope> ,然后刷新包,发现真可以启动了,暂时先记录下,这个还是容器依赖的问题,要让springboot内置tomcate和web生效才行,没时间,就到这把,记录下
HTML5 websockets与PHP websockets vs node.js websockets?
我想知道的是三者之间有什么区别,我的意思是我不想学习所有这三者,如果有的话比其他人好.
解决方法
io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket...
Caused by: java.lang.ClassCastException:
io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket.server.WsServerContainer
at org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy.getContainer(TomcatRequestUpgradeStrategy.java:84)
at org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy.getContainer(TomcatRequestUpgradeStrategy.java:47)
at org.springframework.web.socket.server.standard.AbstractStandardUpgradeStrategy.getSupportedExtensions(AbstractStandardUpgradeStrategy.java:88)
at org.springframework.web.socket.server.support.AbstractHandshakeHandler.doHandshake(AbstractHandshakeHandler.java:272)
at org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest(WebSocketHttpRequestHandler.java:166)
... 56 common frames omitted
问题产生原因是:pom 引入的依赖包 undertow 与 websocket 依赖包中的 tomcat 冲突了导致,如下:
<!--undertow-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--websocket依赖包中本身也依赖tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
解决方案:
<!--websocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
今天的关于Spring WebSockets @SendTo映射中的路径变量和spring路径映射注解的分享已经结束,谢谢您的关注,如果想了解更多关于#翻译# Spring 4.0 中的 WebSocket 架构、Error creating bean with name ''webSocketServer''(springboot 整合 webstock)、HTML5 websockets与PHP websockets vs node.js websockets?、io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket...的相关知识,请在本站进行查询。
本文标签: