【发布时间】:2017-08-27 16:00:02
【问题描述】:
我使用 Netty 作为简单的 websocket 服务器,我希望能够使用 netty 进行通信
第二个使用 HTTP 作为客户端的 netty 服务器。
怎么办?
现在我的初始化程序看起来像这样:
public class PDServerInitializer extends ChannelInitializer<Channel> {
private final ChannelGroup group;
public PDServerInitializer(ChannelGroup group) {
this.group = group;
}
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(64 * 1024));
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpRequestHandler("/ws"));
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast(new TextWebSocketFrameHandler(group));
}
}
public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private final ChannelGroup group;
public TextWebSocketFrameHandler(ChannelGroup group) {
this.group = group;
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
ctx.pipeline().remove(HttpRequestHandler.class);
group.add(ctx.channel());
} else {
super.userEventTriggered(ctx, evt);
}
}
@Override
public void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
msg.retain();
TextWebSocketFrame frame = (TextWebSocketFrame) msg;
String jsonRequest = frame.text();
//I WAHT TO SENT THE STRING jsonRequest to NETTY Server 2 as client from here
}
}
服务器端工作得很好,我想将字符串“jsonRequest”作为 http 请求发送到第二个 netty,并从这个特定的服务器接收答案
【问题讨论】:
-
你到底是什么意思?是否要向其他服务器发出传出 http 请求?
-
是的,我发送 http 请求
标签: netty