问题
The case is the following:
After as clients A
and B
established connections with a server via RSocket
protocol, each of the clients could be notified with their own event(s) (event A
or event B
) to trigger some action on a client (event X -> action on client X
).
Thanks
回答1:
You could achieve it with setup payload.
Server:
@Controller
public class ServerController {
private static final Map<String, RSocketRequester> REQUESTER_MAP = new HashMap<>();
@ConnectMapping("client-id")
void onConnect(RSocketRequester rSocketRequester, @Payload String clientId) {
rSocketRequester.rsocket()
.onClose()
.subscribe(null, null,
() -> REQUESTER_MAP.remove(clientId, rSocketRequester));
REQUESTER_MAP.put(clientId, rSocketRequester);
}
}
Client:
Mono<RSocketRequester> rSocketRequesterMono(
RSocketRequester.Builder rSocketRequesterBuilder, // preconfigured bean
RSocketMessageHandler rSocketMessageHandler, // preconfigured bean
URI webSocket, String clientId) {
return rSocketRequesterBuilder
.rsocketFactory(rsocketFactory -> rsocketFactory
.addSocketAcceptorPlugin(socketAcceptor ->
rSocketMessageHandler.responder()))
.setupRoute("client-id")
.setupData(clientId)
.connectWebSocket(webSocket);
}
Now you can get RSocketRequester by client on the server side and call specific client.
来源:https://stackoverflow.com/questions/58895471/spring-5-webflux-server-push-notifications-via-rsocket-protocol