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

6.10 protobuf.js decode failed #553

Closed
XiaomingZhang opened this issue Dec 13, 2016 · 7 comments
Closed

6.10 protobuf.js decode failed #553

XiaomingZhang opened this issue Dec 13, 2016 · 7 comments
Labels

Comments

@XiaomingZhang
Copy link

Hello
My server is PHP, use google protobuf php lib, client use protobuf.js. use http post method .

Php server return protobuf encode string data, client receive success. receive data length and php send data length the same.

I use StringToUint8Array , string conver to array, then use protobuf.js decode ,

function StringToUint8Array(str)
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return bufView;
}

My .proto file

syntax = "proto3";
package login;
message LoginResponse {
uint32 code = 1;
string unick = 2;
}

But the LoginResponse.code < 128 is OK, if code == 129 the decode is change 253
The string type is the same, the string length == 129, error is

Uncaught RangeError: index out of range: 6 + 253 > 135

I think the question is Hight-order and Low-order of a uint32 ?

Then I try use js encode and decode protobuf is OK!!!

JS encode data the same to server, and all print to console

JS encode:

[8, 129, 1, 18, 129, 1, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115…]

receive from server and StringToUint8Array

[8, 253, 1, 18, 253, 1, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115…]

How can I do it, the protobuf.js has method from php encode protobuf string data conver to uint8array ? Thanks a lot !

@dcodeIO
Copy link
Member

dcodeIO commented Dec 13, 2016

Haven't looked over this in detail, but maybe related: https://github.com/dcodeIO/protobuf.js/wiki/How-to-read-binary-data-in-the-browser-or-under-node.js%3F

@dcodeIO dcodeIO added the unsure label Dec 13, 2016
@XiaomingZhang
Copy link
Author

Indeed, but I failed !
PHP add code

header('Content-Type: application/octet-stream');
OR
header('Content-Transfer-Encoding: binary');

Js client XHR.response is ArrayBuffer type, but content is { } .

Or I where do wrong ?
PHP server has sample code?

Thanks a lot!

@dcodeIO
Copy link
Member

dcodeIO commented Dec 13, 2016

I haven't used the php library myself, unfortunately. Isn't there a safer way to work with buffers in PHP, instead of converting hence and forth from/to strings? String conversion often results in corrupted data due to the underlying encoding used (i.e. utf8, utf16le).

@XiaomingZhang
Copy link
Author

JS client change code

XMLHttpRequest: responseType="arraybuffer"

I tried a lot of methods
I can't receive any data from server.
If remove responseType , I can receive protobuf string data , but can't decode.

Thanks a lot!

@dcodeIO
Copy link
Member

dcodeIO commented Dec 14, 2016

On the PHP side, you have to make sure that it outputs proper binary data. Then, using header('Content-Type: application/octet-stream'); seems right to me.

@XiaomingZhang
Copy link
Author

Thanks for your reply !
I cant test success, May be I don't know how to use. I have begun to use Google/protobuf/js and success.
But I really, really thank you very much for you help !

@dcodeIO dcodeIO closed this as completed Dec 15, 2016
@dcodeIO
Copy link
Member

dcodeIO commented Dec 15, 2016

You are welcome! Feel free to reopen when necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants