0

I'm building a multiplayer game in HTML5. I've done some reading on sending binary data through WebSockets via ArrayBuffers like Uint8Array as I'm like to avoid as much overhead as possible (so no JSON). However, I'm still lost on how to actually send specific variable types (especially 1 bit Booleans) through the WebSocket.

For example, I want to send 5 booleans and 3 numbers (precision to be determined later) via binary data in 1 message (so no multiple sends). In this case:

  1. Number - Frame Number
  2. Boolean - W
  3. Boolean - A
  4. Boolean - S
  5. Boolean - D
  6. Boolean - Mouse Down
  7. Number - Mouse X
  8. Number - Mouse Y

How would I approach this? Could I put it all into a Uint8Array? Isn't that a byte? What if I only need say 5 bits and not 8, would it still take up 8? How do append booleans to and extract booleans from the ArrayBuffer? Or am I using the wrong approach altogether?

Thanks for the help.

1 Answer 1

1

you can use DataView which is more easy to use to write data stream.

https://developer.mozilla.org/en-US/docs/Web/API/DataView?redirectlocale=en-US&redirectslug=Web%2FJavaScript%2FTyped_arrays%2FDataView

Sign up to request clarification or add additional context in comments.

1 Comment

Was looking through my questions and realized that I missed your answer. Sorry for the delay and thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.