私は、同じメッセージをエコーサーバーに送信し続け、受信バイト数をカウントするWebSocketクライアントを構築しました。サーバは、次のようなメッセージを返します。netty LoggingHandlerは、1つのフレームにマルチファイルメッセージを出力し、長すぎるとカウントします。
WebSocketData [受信メッセージ]
をしかし、私はLoggingHandlerによって印刷されたログを確認すると、受信したメッセージは、一緒にエコーサーバースティックで送信します。したがって、LoggingHandlerの前にBytesCountingHandlerを置くと、1つのメッセージのカウント結果が通常よりも大きすぎます。
私は質問したい:これは間違っているのですか?
私の予備的なクラスは以下の通りです:
が接続してメッセージを送信します。
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
}
p.addLast(
new BytesCountHandler(),
new LoggingHandler(LogLevel.INFO),
new HttpClientCodec(),
new HttpObjectAggregator(8192),
handler);
}
});
Channel ch = b.connect(uri.getHost(), port).sync().channel();
handler.handshakeFuture().sync();
for (;;) {
String msg = "hello websocket";
WebSocketFrame frame = new TextWebSocketFrame(msg);
ch.writeAndFlush(frame);
}
BytesCountHandler:LoggingHandlerなし
public class BytesCountHandler extends ChannelDuplexHandler {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
super.write(ctx, msg, promise);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(countMessage(msg));
super.channelRead(ctx, msg);
}
private int countMessage(Object msg) {
if (msg instanceof ByteBuf) {
return countBytes((ByteBuf) msg);
} else if (msg instanceof ByteBufHolder) {
return countByteBufHolder((ByteBufHolder) msg);
}
return 0;
}
private int countBytes(ByteBuf buf) {
return buf.readableBytes();
}
private int countByteBufHolder(ByteBufHolder bufHolder) {
return countBytes(bufHolder.content());
}
ログ:
129
WebSocket Client connected!
320
96
32
32
32
192
160
320
96
448
448
をLoggingHandlerでログインします。
...
WebSocket Client connected!
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler logMessage
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] WRITE(21B)
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 81 8f 1f 79 b5 46 77 1c d9 2a 70 59 c2 23 7d 0a |...y.Fw..*pY.#}.|
|00000010| da 25 74 1c c1 |.%t.. |
+--------+-------------------------------------------------+----------------+
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler flush
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] FLUSH
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler logMessage
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] RECEIVED(32B)
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000010| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
+--------+-------------------------------------------------+----------------+
...
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler logMessage
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] RECEIVED(512B)
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000010| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|00000020| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000030| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|00000040| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000050| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|00000060| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000070| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|00000080| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000090| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|000000a0| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|000000b0| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|000000c0| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|000000d0| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|000000e0| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|000000f0| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|00000100| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000110| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
|00000120| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[|
|00000130| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]|
...
私ははっきり
おかげ
をdiscribedと思っているあなたは、低レベルのネットワークデータではなく、あなたのフレームの内容を数えている
おかげで実際に私が数えたいですHTTPとWSのレイヤーでラッパー、私はちょうどその方法は動作しません混乱。 – ayw
正確なバイトを数えることは本当に難しいです。なぜなら、TCPがすべての受信バッファを特定の事実によって互いに追加するからです。http://netty.io/wiki/user-guide-for-4.x.html#dealing -with-a-stream-based-transportなぜこのようなことが起こるのですか? – Ferrybig
入手してください、ありがとう! – ayw