ctx.channel().write(new TextWebSocketFrame("hello"))didnot not welcome on the client side, but ctx.channel().writeAndFlush(new TextWebSocketFrame("hello"))did.
ctx.channel().write(new TextWebSocketFrame("hello"))
ctx.channel().writeAndFlush(new TextWebSocketFrame("hello"))
What is the difference between the two? ctx- it is ChannelHandlerContext.
ctx
ChannelHandlerContext
According to the document
http://netty.io/4.0/api/io/netty/channel/Channel.html
channel.writeAndFlush(msg);
is a shortcut for
channel.write(msg); channel.flush();
And flush()flushes (writes) all pending buffers.
flush()
, , , , , , , . , , flush(), , . , , , , flush() , , . writeAndFlush , , , .
, , Netty write() flush() flushes. , BufferedOutputStream.
write()
BufferedOutputStream
So the question is: when to flush()? The answer is to complete the sending of the request or response if you are not going to close the channel. For example, in the client, deserves all the request cards, flash, and then reads the answer. On the server, send all parts of the response and flash.