【发布时间】:2015-09-29 12:14:37
【问题描述】:
我已经创建了这个 Websocket 项目Spring Websocket,它工作得非常好。 我将在我的项目中介绍这个例子。在那里,我要求(聊天)组可以动态创建或删除/销毁。
在我的 WebsocketConfig- 类端点可以通过以下方式静态添加:
registry.addEndpoint("/hello").withSockJS(); (also see below)
是否有可能动态添加端点? 我的用例是我有属于一家或多家公司的公司和员工:
n m (m:n relation)公司员工
并且可以动态创建公司(通过单击“创建”按钮)。然后可以将之前注册的员工添加到公司。 因此,这意味着如果创建了一家公司(并且至少向公司添加了 2 名员工),则应该添加一个端点。
我很高兴在这个方向上提供任何有用的答案。 非常感谢!
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
// Prefix for messages FROM server TO client
config.enableSimpleBroker("/topic");
// Prefix for messages FROM client TO server
config.setApplicationDestinationPrefixes("/app");
// /app wird beim client - sendName verwendet: stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name
// }));
}
@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}
[编辑] 向多个客户端发送消息,但不是向所有客户端发送消息。这是我下面的当前代码。发送给具有相同 ID 的所有人都可以正常工作,但我不知道如何将消息发送到例如4个客户。 感谢您的帮助!
@MessageMapping("/chat/{institutionId}")
public void greeting(@DestinationVariable String institutionId, final GreetingHelloMessage message) throws Exception {
final Greeting greeting = new Greeting(institutionId, "Hello " + institutionId + " - " + message.getName());
simpMessagingTemplate.convertAndSend("/topic/chat/" + institutionId, greeting);
}
【问题讨论】:
-
我发现了这个:[在此处输入链接描述][1] [1]:stackoverflow.com/questions/21192091/…