WebFlux服务器推送通知

WebFlux服务器推送通知

本文介绍了通过RSocket协议的Spring 5 WebFlux服务器推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下:

当客户端AB通过RSocket协议与服务器建立连接后,可以使用各自的事件(event Aevent B)通知每个客户端以触发某些事件对客户端(event X -> action on client X)的操作.

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).

谢谢

推荐答案

您可以通过设置有效负载来实现.

You could achieve it with setup payload.

服务器:

@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);
    }
}

客户:

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);
}

现在,您可以在服务器端通过客户端获取RSocketRequester并调用特定的客户端.

Now you can get RSocketRequester by client on the server side and call specific client.

这篇关于通过RSocket协议的Spring 5 WebFlux服务器推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 09:39