oatpp / oatpp-websocket

oatpp-websocket submodule.
https://oatpp.io/
Apache License 2.0
78 stars 32 forks source link

example for multiframe/ChunkedBuffer usage #18

Closed r2dliu closed 4 years ago

r2dliu commented 4 years ago

Hello,

I am trying to send a message through a websocket from my server to a client. I want to send a rather large message, a struct that contains something on the order of several megabytes worth of data.

I see a few methods and the example implementation for sending one frame of text/small strings, but am not sure of the correct way to do multiframe or large pieces of fixed size byte data.

Could you provide an example or point me in the right direction?

bamkrs commented 4 years ago

Hello there,

what are you trying to archive? In general, WS are a stream and a single WS-Frame can grow up to 2^63 bytes (9,223,372,036,854,775,807 bytes ~ 9.22 exabytes). So its only a matter of how you are managing your data. So if you want to chunk your data, just send them in chunks of your liking.

Can you elaborate more on your use-case so we can come up with an nice idea?

lganzzzo commented 4 years ago

Hello,

Also, you can manually send a multi-frame message:


ws->sendOneFrame(false /*fin*/, Frame::OPCODE_TEXT         /*opcode*/, "Hello"  /*first message part*/);
ws->sendOneFrame(false /*fin*/, Frame::OPCODE_CONTINUATION /*opcode*/, " "      /*continuation message part*/);
ws->sendOneFrame(true  /*fin*/, Frame::OPCODE_CONTINUATION /*opcode*/, "World!" /*last message part*/);

And in the future, we plan to introduce a convenience API for this.

Regards, Leonid

r2dliu commented 4 years ago

nvm, I have gotten it to work, just misunderstood how oatpp::String worked under the hood.

Worked once I started using getData() to retrieve directly the pointer to the bytes received when sending a binary frame.

lganzzzo commented 4 years ago

Hey @r2dliu ,

Please also take into account that the approach mentioned in the previous comment (with reinterpreting bytes to/from struct/string) is working for sending/receiving messages between machines with the same byte-order only. And will fail for communication between machines with different byte-order.

In order to have consistent and portable serialization/deserialization on machines with different byte-orders, you may use oatpp DTOs with custom ObjectMapper.

Regards, Leonid