Spring 5 WebFlux server push notifications via RSocket protocol

旧时模样 提交于 2020-03-23 07:55:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!