mercredi 15 août 2018

Missing bytes in BinaryWebSocketFrame

I am learning websocket in netty. Here is part of my websocket handler

@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();
        }

I use a websocket in webclient to send data to my server and print out the size of the 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);

}

I found that sometimes the message from the client part is inconsistent with the message I received in netty server: client console message:

2214:84794

(when count = 2214, the event.data.size = 84794) However, my server side:

 [java] 2214
 [java]  readable bytes of message78976
 [java] buffer position:0
 [java] buffer limit:78976
 [java] buffer capacity:78976

This happens without any rule, just unexpectedly. I think it has something to do with the low level memory management with netty. Any information will be appreciated pretty much! Thanks!




Aucun commentaire:

Enregistrer un commentaire