Skip to content

6.10 protobuf.js decode failed #553

@XiaomingZhang

Description

@XiaomingZhang

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 !

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions