我尝试从 JavaScript 向 Netty 4 HTTP 服务器发送异步请求,但没有得到响应。 这是我使用的代码。
Netty服务器初始化:
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(2);
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new HttpHelloWorldServerInitializer());
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
管道初始化 - HttpHelloWorldServerInitializer
ChannelPipeline p = ch.pipeline();
p.addLast("logger", new LoggingHandler(LogLevel.INFO));
p.addLast("decoder", new HttpServerCodec());
p.addLast("handler", new HttpHelloWorldServerHandlerIn());
处理程序 - HttpHelloWorldServerHandlerIn
public class HttpHelloWorldServerHandlerIn extends SimpleChannelInboundHandler<Object> {
private static final byte[] CONTENT = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK,
Unpooled.wrappedBuffer(CONTENT));
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(CONNECTION, Values.KEEP_ALIVE);
ctx.write(response);
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
}
HTML页面
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Netty test</title>
<script type="text/javascript">
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
document.getElementById("myDiv2").innerHTML = xmlhttp.readyState + " " + xmlhttp.status + " " + xmlhttp.getAllResponseHeaders();
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "http://localhost:8081/test", true);
xmlhttp.send(null);
}
</script>
</head>
<body>
<div id="myDiv"><h2>Text here</h2></div>
<div id="myDiv2"><h2></h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
在 HTML 页面中,我看到 xmlhttp.readyState=4、xmlhttp.status=0、xmlhttp.getAllResponseHeaders() 为空。
Netty 日志显示我发送到 JavaScript 的正确转储内容
2013 4:15:19 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x619b60f9, /0:0:0:0:0:0:0:1:59641 => /0:0:0:0:0:0:0:1:8081] WRITE(100B)
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d |HTTP/1.1 200 OK.|
|00000010| 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 |.Content-Type: t|
|00000020| 65 78 74 2f 70 6c 61 69 6e 0d 0a 43 6f 6e 74 65 |ext/plain..Conte|
|00000030| 6e 74 2d 4c 65 6e 67 74 68 3a 20 31 31 0d 0a 43 |nt-Length: 11..C|
|00000040| 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 2d |onnection: keep-|
|00000050| 61 6c 69 76 65 0d 0a 0d 0a 48 65 6c 6c 6f 20 57 |alive....Hello W|
|00000060| 6f 72 6c 64 |orld |
+--------+-------------------------------------------------+----------------+
和
2013 4:15:19 PM io.netty.handler.logging.LoggingHandler flush
INFO: [id: 0x619b60f9, /0:0:0:0:0:0:0:1:59641 => /0:0:0:0:0:0:0:1:8081] FLUSH
请帮忙!我做错了什么?
最佳答案
我会使用wireshark检查字节是否真的通过写入发送。此外,您还应该检查 write(...) 返回的 ChannelFuture 是否由于某种原因失败。
类似于:
ChannelFuture future = ctx.write(...);
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
System.out.println("success=" future.isSuccess());
if (!future.isSuccess()) {
future.cause().printStracktrace();
}
}
});
关于java - 向 Netty HTTP 服务器发送 ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353875/