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

Binary messages? #6

Open
dtracers opened this issue Dec 29, 2014 · 6 comments
Open

Binary messages? #6

dtracers opened this issue Dec 29, 2014 · 6 comments

Comments

@dtracers
Copy link

I could not find anything about it supporting binary messages. Does this library support mocking of binary messages?

@thoov
Copy link
Owner

thoov commented Jan 7, 2015

@dtracers right now there is no native support for binary messages but it will be on of the next things I work on adding.

@cades
Copy link

cades commented Jun 5, 2015

+1

@thoov
Copy link
Owner

thoov commented Jun 5, 2015

@cades or @dtracers do you have any examples I could use as a test case? If so I will be able to add support quickly

@dtracers
Copy link
Author

dtracers commented Jun 5, 2015

I use protobuf.js.

So a good example test might be sending a hello world protobuf file via
binary.

I don't think I'll be able to come up with one until this weekend. And
maybe not even then.
On Jun 5, 2015 11:01 AM, "Travis Hoover" notifications@github.com wrote:

@cades https://github.com/cades or @dtracers
https://github.com/dtracers do you have any examples I could use as a
test case? If so I will be able to add support quickly


Reply to this email directly or view it on GitHub
#6 (comment).

@nomve
Copy link

nomve commented Nov 17, 2015

I don't know if something changed in the meantime, but this works:

    var binaryDataBuffer = new ArrayBuffer(1),
        binaryDataArray = new Uint8Array(binaryDataBuffer);

    clientInstance.onmessage = function(event) {
        var data = new Uint8Array(event.data);
        QUnit.equal( data[0], 1, "didn't receive the right data" );
    };

    binaryDataArray[0] = 1;
    serverInstance.send(binaryDataBuffer);

@jocull
Copy link

jocull commented Mar 21, 2020

It seems to work as long as you consistently use ArrayBuffer and related views on both sides.

If you send a Buffer type from Node something seems to get crossed and an empty ArrayBuffer appears on the other side.

server.on('connection', socket => {
  socket.on('message', (msg) => {
    const encodedAsNodeBuffer = myBufferGenerator();
    const newBuffer = new ArrayBuffer(encodedAsNodeBuffer.byteLength);
    const newBufferView = new Uint8Array(newBuffer);
    newBufferView.set(encodedAsNodeBuffer, 0);
    socket.send(newBuffer);
  }
}

The protobufjs code generator seems to use Buffer on the Node side, so something to be aware of if you are using that.

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

5 participants