-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
how to use protobuf with websocket #689
Comments
Make sure to set Send out the backing ArrayBuffer of the Uint8Array returned by SomeMessage.encode(someMessageInstance).finish() over your WebSocket (slice it if necessary), and decode the ArrayBuffer on the other side through SomeMessage.decode(new Uint8Array(receivedArrayBuffer)) Sending, for example: var buffer = SomeMessage.encode(someMessageInstance).finish();
mySocket.send(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.length)); If you are planning to use WebSockets with node.js, there is the ws module (see: Sending binary data). |
thanks very much! your js'code is in dist/nginx/html/ dir. |
Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary! |
Hello, i might have some misunderstanding about websockets but let me ask you, when ws.send(someMessage) has been called in clientside, how can i verify which message instance has been sent to servers.
|
You could either wrap your messages in an envelope message, for example ... message Envelope {
oneof kind {
SomeMessage a = 1:
SomeOtherMessage b = 2;
}
} or implement a custom header for your purpose. |
var buf = SomeMessage.encode(someMessageInstance).finish(); |
Hello, actually i meet a similar issue. In my javascript client, i get the data which type is "string" from my C++ server by webSocket. And i can not deserialize it directly. And i have tested to turn it to arraybuffer which doesn't works. function stringToArrayBuffer(str) { And i can't find the function "encode()" in javascipt also there seems no funtion to set binaryType . Instead, "binaryType" is a property, and i print it , i get "nodebuffer". So how to set the recieved data from server as 'arraybuffer'`and then i can deserialize it in protobuf. Here is my source. client.js sock.on("error", function(err) { sock.on("close", function() { sock.on("message", function(data) { webCut.proto |
how to use protobuf with websocket, i want to sent binary data to server use websocket. thanks!
The text was updated successfully, but these errors were encountered: