我正在学习netty网络套接字。这是我的websocket处理程序的一部分
@Override
public void channelRead (ChannelHandlerContext ctx, Object msg) throws IOException, MkvElementVisitException{
if (msg instanceof WebSocketFrame) {
if (msg instanceof BinaryWebSocketFrame) {
System.out.println(messageIndex);
messageIndex++;
// transfer WebSocketFrame to bytebuffer
ByteBuf temp = ((BinaryWebSocketFrame)msg).content();
int readable = temp.readableBytes();
System.out.print(" readable bytes of message");
System.out.println(temp.readableBytes());
ByteBuffer buf = ByteBuffer.allocate(temp.readableBytes());
temp.readBytes(buf);
buf.flip();
System.out.print("buffer position:");
System.out.println(buf.position());
System.out.print("buffer limit:");
System.out.println(buf.limit());
System.out.print("buffer capacity:");
System.out.println(buf.capacity());
ByteBuffer duplicate = buf.asReadOnlyBuffer();
String filename = "generatedvideo_09.webm";
try {
String dir = "Users/generatedWebm/";
File d=new File(dir);
if (!d.exists()) {
d.mkdirs();
}
writeDataToFile(filename, d, duplicate);
} catch (FileNotFoundException e){;}catch (IOException e){;}
} else if (msg instanceof CloseWebSocketFrame) {
ctx.write(new CloseWebSocketFrame());
ctx.close();
}
我在webclient中使用websocket将数据发送到我的服务器并打印出blob的大小:
mediaRecorder.ondataavailable = function(event) {
if (event.data && event.data.size > 0) {
recordedBlobs.push(event.data);
console.log(count + ':'+ event.data.size);
count = count + 1;
ws.send(event.data);
}
我发现有时来自客户端的消息与我在Netty服务器中收到的消息不一致: 客户端控制台消息:
2214:84794
(当count = 2214时,event.data.size = 84794) 但是,我的服务器端:
[java] 2214
[java] readable bytes of message78976
[java] buffer position:0
[java] buffer limit:78976
[java] buffer capacity:78976
这种情况没有任何规则,只是出乎意料。 我认为这与netty的低级内存管理有关。 任何信息将不胜感激! 谢谢!