qum*_*uma 6 spring spring-websocket
我创建了这个 Websocket 项目Spring Websocket,它工作得很好。我将在我的项目中介绍这个例子。我要求可以动态创建或删除/销毁(聊天)组。
在我的 WebsocketConfig- 类端点中,可以通过以下方式静态添加:
registry.addEndpoint("/hello").withSockJS(); (also see below)
Run Code Online (Sandbox Code Playgroud)
是否有可能动态添加端点?我的用例是我有属于一个或多个公司的公司和员工:
Run Code Online (Sandbox Code Playgroud)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();
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑] 向多个客户端发送消息,但不是向所有客户端发送消息。这是我当前的代码如下。使用相同的 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5058 次 |
| 最近记录: |