Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataView read wrong value on msgpack deserialized data #26

Open
dukelec opened this issue Feb 1, 2021 · 3 comments
Open

DataView read wrong value on msgpack deserialized data #26

dukelec opened this issue Feb 1, 2021 · 3 comments

Comments

@dukelec
Copy link

dukelec commented Feb 1, 2021

The data was send from Python websocket server (type: bytes), and deserialize by your msgpack (version: e33d6aa).

Screenshot_2021-02-01_22-58-13

@dukelec dukelec changed the title DataView read wrong value of msgpack deserialized data DataView read wrong value on msgpack deserialized data Feb 1, 2021
@ygoe
Copy link
Owner

ygoe commented Feb 1, 2021

I don't see what you really did and what data you have, or even where you call the msgpack code. I'd think that slice(0) is kind of a no-op. Maybe a browser issue? I have no idea what to do here. I can't debug anything without proper details.

@dukelec
Copy link
Author

dukelec commented Feb 2, 2021

Firefox and Chrome have the same result.

Here is small test demo for you: dbg_msgpack.zip

Screenshot_2021-02-02_11-27-20

Python code snippet of send one message to web page when websocket connected:

async def serve(ws, path):
    try:
        print(f'ws: connect, path: {path}')
        while True:
            dat = b'\x00\x00\x00\x00\x00'
            print(f'send test dat: {dat}')
            msg = umsgpack.packb(dat)
            print(f'msg: {msg}')
            await ws.send(msg)
            break # only send one pkg
    
    except websockets.exceptions.ConnectionClosed:
        print(f'ws: exception, path: {path}')
    
    print(f'ws: disconnect, path: {path}')

Full javascript code:

async function blob2dat(blob) {
    let ret;
    await new Promise(resolve => {
        new Response(blob).arrayBuffer().then(buf => {
            ret = new Uint8Array(buf);
            resolve();
        });
    });
    return ret;
}


function init_ws() {
    let ws_url = `ws://${window.location.hostname}:8910/`;
    let ws = new WebSocket(ws_url);
    
    ws.onopen = async function(evt) {
        console.log("ws onopen");
    }
    ws.onmessage = async function(evt) {
        let msg = await blob2dat(evt.data);
        var dat = msgpack.deserialize(msg);
        console.log("Received dat", dat);
        
        let dv = new DataView(dat.buffer);

        dat2 = dat.slice(0); // dataview return wrong value without this
        let dv2 = new DataView(dat2.buffer);
        
        console.log('ng', dv.getUint32(0, true), dat.slice(0, 4));
        console.log('ok', dv2.getUint32(0, true), dat2.slice(0, 4));

    }
    ws.onerror = function(evt) {
        console.log("ws onerror: ", evt);
        document.body.style.backgroundColor = "gray";
    }
    ws.onclose = function(evt) {
        console.log('ws disconnected');
        document.body.style.backgroundColor = "gray";
    }
}

init_ws();

The number 1476 of the ng output, in hex is 0x05c4, is exactly the data before deserialize.

@ygoe
Copy link
Owner

ygoe commented Feb 14, 2021

Isn't this a bug in DataView? Sorry, I don't know what to do here. After looking up what DataView.getUint32 does, I believe that there should be no difference between the two calls. Please ask elsewhere, I can't help here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants