2

案例如下:

在作为客户端AB通过协议与服务器建立连接后RSocket,每个客户端都可以通过自己的事件(event Aevent B)通知,以触发客户端上的某些操作(event X -> action on client X)。

谢谢

4

1 回答 1

3

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

服务器:

@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 并调用特定的客户端。

于 2019-11-17T08:11:09.380 回答