Я пытаюсь отправить запрос async с JavaScript на HTTP-сервер Netty 4 и не получить ответ. Вот код, который я использую.Отправить запрос ajax на 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();
}
инициализации Pipeline - HttpHelloWorldServerInitializer
ChannelPipeline p = ch.pipeline();
p.addLast("logger", new LoggingHandler(LogLevel.INFO));
p.addLast("decoder", new HttpServerCodec());
p.addLast("handler", new HttpHelloWorldServerHandlerIn());
Handler - 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() пуст.
Нетти журнал показать правильный дамп, что я отправить 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
Пожалуйста, помогите! Что я делаю неправильно?
Norman, я получаю успех = истина , – smie
И wirehark показывают все байты, которые я отправляю. Но браузер не показывает ответ – smie
Это означает, что он был отправлен по кабелю .. Может быть, debuggin в браузере помогает –