2017-11-04 11 views
1

私はSocket.IO-client Javaからデータを取得するSpring WebFluxエンドポイントを実装しています。Socket.IO-client Java用のReactor Fluxプロキシー

Fluxストリームに入ってくるデータを収集する方法がわかりません。新しいFluxをいくつか作成し、それを受信データに登録することはできますか?アドバイスをありがとう。

@GetMapping("/streaming", produces = MediaType.APPLICATION_STREAM_JSON_VALUE) 
    public Flux<MyRecourse> getStreaming() { 

    URI uri = URI.create("http://localhost/socket.io"); // client 
    Socket socket = IO.socket(uri); 

    socket.on("event", args -> {  
     JSONObject obj = (JSONObject)args[0]; 
     MyRecourse recource = MyRecourse.create(obj); 

     // how to put this recource into Flux stream? 
    }); 

    return fluxStreamOfRecources; 

} 

答えて

2

あなたは、イベントリスナーからFluxを生成するFlux.create()を使用することができます。

Flux.<MyResource>create(emitter -> { 

    URI uri = URI.create("http://localhost/socket.io"); // client 
    Socket socket = IO.socket(uri); 

    socket.on("event", args -> {  
     JSONObject obj = (JSONObject)args[0]; 
     MyResource resource = MyResource.create(obj); 
     emitter.next(resource); 
    }); 

    // subscribe on error events 
    socket.on(Socket.EVENT_CONNECT_ERROR, args -> {  
     // get error 
     emitter.error(throwable); 
    }); 

    // unsubscribe from events when the client cancels 
    emitter.onDispose(() -> { 
     // disconnect from socket 
     // socket.off(...) 
    }); 
}); 
関連する問題