2
這可能很簡單,但我無法弄清楚。基於Netty 4的http服務器導致http客戶端掛起其響應。它設法通過其響應有效載荷發送(如使用curl作爲客戶端所觀察到的),但客戶端似乎沒有意識到響應已完成並且無限期地等待它完成。使用curl觀察,以及firefox和chrome。Netty http服務器響應
僅當我修改代碼以關閉通道(channel.close,如下面的內聯)時,則客戶端確認響應已完成。否則,他們只是繼續等待它完成。我希望頻道保持開放狀態,以便下一個客戶端請求不需要打開新連接(我希望保持活動行爲),因此關閉頻道似乎不太合理。所以我不確定服務器如何將響應標記爲結束 - 不關閉連接。
服務器代碼:
val response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
val buf = new StringBuilder
buf.append("hello")
response.data.writeBytes(Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8))
ctx.write(response).addListener(new ChannelFutureListener(){
def operationComplete(channelFuture: ChannelFuture){
if (channelFuture.isSuccess){
println("server write finished successfully")
//channelFuture.channel.close <===== if uncommented, clients receive the response, otherwise they just keep waiting forever
}
else
println ("server write failed: " + channelFuture.cause + "\n" + channelFuture.cause.getStackTraceString)
}
})
我在想什麼?
您是否正在發送內容長度標題? – Kylar 2013-02-20 15:43:53
不是,我沒有積極添加任何標題。我是不是該? – matanster 2013-02-20 16:43:49
是的。如果沒有Content-Length標題,客戶不知道何時停止閱讀。如果您的內容只是「你好」,然後添加:Content-Length:5. – Kylar 2013-02-20 16:47:41