我正在试验Spring 4 WebSocket STOMP应用程序.有没有办法在每个用户都有唯一会话ID的情况下回复单个未经身份验证的用户?现在我只能广播消息或直接发送给经过身份验证的用户.
@Controller
public class ProductController {
@MessageMapping("/products/{id}")
@SendTo("/topic") // This line makes return value to be broadcasted to every connected user.
public String getProduct(@DestinationVariable int id) {
return "Product " + id;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring websockets和stomp.js创建一个示例聊天应用程序,我正在使用tomcat 7.54,但在运行应用程序时,我在浏览器发出xhr请求时遇到了异步支持的错误.
服务器信息:Apache Tomcat/7.0.54 Servlet版本:3.0 JSP版本:2.2 Java版本:1.7.0_25
在web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<async-supported>true</async-supported>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Run Code Online (Sandbox Code Playgroud)
调度员servlet.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<context:component-scan base-package="hello" />
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker> …Run Code Online (Sandbox Code Playgroud) 有没有办法获取STOMP客户端IP地址?我正在拦截入站通道,但我看不到任何方法来检查IP地址.
任何帮助赞赏.
我将 Spring WebSockets 与 STOMP 和 SockJS 用于前端。它工作正常,但我还有另一个困难。
这是后端代码:
@MessageMapping("/showAccountlist")
@SendTo("/topic/accounts")
public Account createPublishAccount(String name) throws Exception {
return new Account(name);
}
Run Code Online (Sandbox Code Playgroud)
这是前端代码,运行良好,所有消息都发布到所有客户端。
stompClient.send("/app/showAccountlist", {}, name);
Run Code Online (Sandbox Code Playgroud)
但是当我从我的 java 后端调用我的后端方法时,使用方法名称
createPublishAccount("Carlos");
Run Code Online (Sandbox Code Playgroud)
似乎消息没有发布。有什么解决办法吗?或者这不是它的工作方式,它仅在通过 SockJS 触发时才有效?
这是我的网络配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/showAccountlist").withSockJS();
}
Run Code Online (Sandbox Code Playgroud)
}
从4月开始,我们支持STOMP(子)协议WebSocket.我明白的好处WebSocket相比,HTTP使用和使用与收益STOMP比WebSocket,但我想了解以下内容:
直接使用stomp协议与MB交谈是否有任何性能优势(如RabbitMQ或Kafka - 可能在将来)
STOMP除了处理客户端连接到服务器/ MB所需的握手之外,使用作为子协议而不是web套接字有什么好处
我正在使用本指南来实现一个简单的 Stomp 客户端:
WebSocketClient webSocketClient = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
stompClient.setTaskScheduler(taskScheduler); // for heartbeats
stompClient.setMessageConverter(new StringMessageConverter());
StompSessionHandler sessionHandler = new MySessionHandler();
stompClient.connect("ws://server/endpoint", sessionHandler);
// WAITING HERE
Run Code Online (Sandbox Code Playgroud)
当连接完成时,它应该MySessionHandler异步报告:
public class MySessionHandler extends StompSessionHandlerAdapter
{
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders)
{
// WAITING FOR THIS
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是:线路WAITING HERE应该如何等待线路WAITING FOR THIS?是否有特定的 Spring 方式来执行此操作?如果不是,哪种通用 Java 方式最适合这里?
我是春天的新手
我有这个课:
public class Server extends TextWebSocketHandler implements WebSocketHandler {
WebSocketSession clientsession;
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
clientsession = session;
}
Run Code Online (Sandbox Code Playgroud)
我需要在clientsession上检测到客户端断开连接。实现ApplicationListener,但不清楚如何注册监听器?我需要在我的web.xml中执行此操作吗?
在 Spring Boot (Websockets) 中
我刚看到这个例子:
messaging.convertAndSendToUser( username, "/queue/notifications",
new Notification("You just got mentioned!"));
Run Code Online (Sandbox Code Playgroud)
这家伙从哪里得到用户名?我找不到任何关于从哪里获得该用户名的提及......
我有一个基于Spring的Websocket-stomp服务器及其SimpleBroker实现(不利用外部代理)。
我想启用STOMP RECEIPT消息。
如何配置代码以自动发送这些代码?
我可以使用通过WebSocketConfigurer配置的websocket,也可以使用@Scheduled()计划任务,而不会出现任何问题。
但是,当我同时使用它们时,java无法编译。
@Scheduled()注释可能会因org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport $ NoOpScheduler.scheduleAtFixedRate()而崩溃
java.lang.IllegalStateException: Unexpected use of scheduler.
at org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport$NoOpScheduler.scheduleAtFixedRate(WebSocketConfigurationSupport.java:123)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleFixedRateTask(ScheduledTaskRegistrar.java:462)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleFixedRateTask(ScheduledTaskRegistrar.java:436)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleTasks(ScheduledTaskRegistrar.java:357)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:332)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:280)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:211)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:102)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:399)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:353)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:887)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
at com.yeadev.JavaSpringBootJARAngularSeed.JavaSpringBootJarAngularSeedApplication.main(JavaSpringBootJarAngularSeedApplication.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Run Code Online (Sandbox Code Playgroud)
@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {
@Override
public void …Run Code Online (Sandbox Code Playgroud) spring-websocket ×10
spring ×9
websocket ×6
stomp ×5
java ×4
spring-boot ×2
concurrency ×1
rabbitmq ×1
servlets ×1
spring-mvc ×1
tomcat ×1