2
クライアントでSSE especificationを使用するようにNettyベースのサーバーを作成しようとしています まず、SimpleChannelInboundHandlerから拡張されたハンドラ(NotifyHandler)を作成し、通知がonNotificationRecibedに到着したときコンテキスト出力チャネルに書き込まれます。初期化子でNetty 4 SSEの更新イベント
private ChannelHandlerContext context = null;
private Publisher p = null;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
this.context = ctx;
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK);
HttpHeaders headers = response.headers();
headers.set(HttpHeaders.Names.CONTENT_TYPE, "text/event-stream");
headers.set(HttpHeaders.Names.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
headers.set(HttpHeaders.Names.PRAGMA, HttpHeaders.Values.NO_CACHE);
headers.set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
ctx.writeAndFlush(response);
Pub.getInstance().suscribe(this);
}
@Override
public void onNotificationRecibed(String type, Map<String, Object> data) {
context.writeAndFlush("event:"+type);
context.writeAndFlush("data:"+data.toString());
context.flush();
}
:
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpRequestDecoder());
pipeline.addLast(new HttpResponseEncoder());
pipeline.addLast(new NotifyHandler());
}
私はそれを動作させるcan't、私はこの上でいくつかの例や使用法を見つけようとストリームが、何も動作していないようにみえます。誰でも私を正しい方向に向けることができますか?私の英語のために申し訳ありません、あなたの時間をありがとう。
あなたは何がうまくいかないのかについていくつか詳しく説明できますか?また、ChannelFutureListenerをwriteAndFlush()によって返されたChannelFutureに追加し、未来が失敗したかどうかを確認する必要があります。 –
ノーマンありがとうございます。問題は、クライアント側がデータを受信していないことです。 ChannelFutureListenerで試してみて、結果を伝えてください。 – tbp