【发布时间】:2018-09-26 04:34:40
【问题描述】:
我在 Client 中使用 ReactJS\redux 和在 Server 中使用 spring-boot-starter-webflux\Reactor\Spring Boot 2 开发了网络聊天。
服务器开发类似于here。
Clint 以这种方式接收消息:
this.clientWebSocket = new WebSocket("ws://127.0.0.1:8080/websocket/chat");
this.clientWebSocket.onopen = function() {
}
this.clientWebSocket.onclose = function() {
}
this.clientWebSocket.onerror = function() {
}
var _this = this;
this.clientWebSocket.onmessage = function(dataFromServer) {
_this.updateMessages(dataFromServer);
}
一条消息将发送给所有客户端。 我如何通过 websockets 向特定的客户端发送消息? 并非全部。
我找到了一些关于 SockJS 的指南,但我想使用 Spring Webflux\Reactor。
更新:
我在服务器中有以下配置:
@Bean
public HandlerMapping webSocketMapping(UnicastProcessor<Event> eventPublisher, Flux<Event> events) {
Map<String, Object> map = new HashMap<>();
map.put("/websocket/chat", new ChatSocketHandler(eventPublisher, events));
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
simpleUrlHandlerMapping.setUrlMap(map);
simpleUrlHandlerMapping.setOrder(10);
return simpleUrlHandlerMapping;
}
所以我从客户端向 /websocket/chat 路径发送消息。
我可以设置这条路径让 Client1 向 /websocket/chat/chat1 发送消息,Client2 向 /websocket/chat/chat2 发送消息吗?所以 /websocket/chat/chat1 和 /websocket/chat/chat2 是不同的聊天。
例如Client1和Client3收到/websocket/chat/chat1的消息,却没有收到/websocket/chat/chat2的消息? p>
聊天的数量可以根据客户的活动动态变化。
【问题讨论】:
标签: java reactjs spring-websocket spring-webflux project-reactor