GullerYA
GullerYA

Reputation: 1776

how to use Jetty's onWebSocketBinary API in a real world application

API signature of the obWebSocketBinary method of WebSocketListener and it's subclasses looks like this:

void onWebSocketBinary​(byte[] payload, int offset, int len)

Let's call the bytes in the payload from offset due len - 'current_bytes'. How should I interpret this API:

If, presumably, the offset due len is not the full content of the payload array, can I consider it as a 'shared' memory used as a buffer for different payloads?

Upvotes: 1

Views: 245

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49545

The API is whole binary messages.

The reason for the offset/len is because the byte buffers are allocated before the entire overall length of the entire websocket message is known.

So the byte buffer sent to you is often larger then the message.

If you want partial messages, then you'll need to know the "fin" (final) flag.

For partial binary, you'd use the WebSocketPartialListener interface and use the method ...

onWebSocketPartialBinary​(java.nio.ByteBuffer payload, boolean fin)

Upvotes: 2

Related Questions