【问题标题】:How to Send a list in spring webflux Reactive websockets如何在 spring webflux Reactive websockets 中发送列表
【发布时间】:2020-04-08 03:48:57
【问题描述】:

我正在使用 spring websockets,我想从存储库中返回一个项目列表

 List<Items> nameList = itemsService.getItems ();

如何在 websocket 会话中发送此列表

 public Mono<Void> handle(WebSocketSession webSocketSession) {
        System.out.println (webSocketSession.getId () + "sessionId" );
        List<Items> nameList = itemsService.getItems ();
        Flux<Items> output = Flux.fromIterable (nameList);
        Mono<Void> send = webSocketSession.
                send (output.map ((Items s) -> webSocketSession.binaryMessage (s)));
        return send;

这是我的处理方法,如何将项目列表发送到 websocket

【问题讨论】:

    标签: java spring-boot websocket spring-webflux


    【解决方案1】:
    Flux<Items> flux = Flux.fromIterable (nameList);
            Flux<WebSocketMessage> webSocketMessageFlux = flux
                    .map(itm ->convertIemsToString(itm)).map (item -> webSocketSession.textMessage (item));
    
    
            return webSocketSession.send (webSocketMessageFlux);
    

    所以首先我将列表转换为通量 然后将其转换为 websocketMessage 然后将 websocket 消息发送到将被推送到客户端的 seesion

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-26
      • 2021-08-04
      • 2019-08-15
      • 2021-02-25
      • 1970-01-01
      • 2018-03-17
      • 2020-12-20
      • 2019-12-04
      相关资源
      最近更新 更多