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

toArrayBuffer question #56

Closed
ucchen opened this issue Sep 16, 2013 · 5 comments
Closed

toArrayBuffer question #56

ucchen opened this issue Sep 16, 2013 · 5 comments
Labels

Comments

@ucchen
Copy link

ucchen commented Sep 16, 2013

Hello!
here is my test code.

var test = new protoObject({
      "id": 130,
      "name": "abc"
});
var buff = test.encode(); 
 var arrayBuffer = buff.toArrayBuffer();
var _uint16Buffer = new Uint16Array(arrayBuffer);

but, get an error when i new Uint16Array. the log said that arrayBuffer is a invalid arguments. why arrayBuffer is an invalid arguments? but, if i do this var _uint8Array = new Uint8Array(arrayBuffer), it is ok. what's wrong of that? arrayBuffer is a ArrayBuffer? if arrayBuffer is a ArrayBuffer, why i can't new Uint16Array?

here is my second test code

var array = new Uint8Array(arrayBuffer);
var str = String.fromCharCode.apply(null, array);
i need to change arrayBuffer to string and send that string to server. but server receive a wrong data(string is right, but int is wrong).
such as when i send this struct to server
var test = new protoObject({
     "id": 130,
     "name": "abc"
}) 

server receive such struct
id: 16706
name: "abc"

id's data isn't right, but string data is ok.
why?

@dcodeIO
Copy link
Member

dcodeIO commented Sep 16, 2013

When sending binary data over the network and no binary-safe mechanism is available, you should encode it to base64 prior to transmission and decode it on the other end.

Regarding the invalid argument error I have no idea yet. Can you explain the difference between the two new Uint16Array calls in more detail?

@ucchen
Copy link
Author

ucchen commented Sep 16, 2013

I have solved this problem. arrayBuffer can't converted to Uint16Array, because the number of bytes of data buffer may be singular, such as arrayBuffer byteLength is 61, it can only converted to Uint8Array, but if arrayBuffer byteLength is 62 or other even, it can converted to Uint16Array or Uint8Array. Actually, We do not know the length of the buffer is even or base. So, we shouldn't use Uint16Array.

String.fromCharCode(), this function will returns a string created by using the specified sequence of Unicode values.but, in my server, it unacceptable string has been encoded, so, i set string to latin1 before i send it to server. So, solve all the problems.

@ucchen ucchen closed this as completed Sep 18, 2013
@kktheballer
Copy link

Ucchen. I am having a similar problem with my code as well. Do you by anychance have the code that you used to send the str via a socket. I am trying to do what you are doing but my python script is not recognizing the string that I am sending to it.

@ucchen
Copy link
Author

ucchen commented Jul 28, 2015

doesn't recognizing the string? can you post more detail?

@kktheballer
Copy link

var commandmessage = new MCM ({
"carrier_freq": 92343.3,
"num_spurs": 33333,
});

               console.log (commandmessage);
               var byteBuffer = commandmessage.encode();
               var buffer = byteBuffer.toArrayBuffer();

chrome.sockets.udp.send(socketId, byteBuffer, "0.0.0.0", 9994, function(sendInfo) {
console.log ("PYTHON SIMULATOR INITIATED!");
console.log("sent " + sendInfo.bytesSent + " bytes");
});
}catch (err) {
console.log ("No data encoded");
}
Above is my code to send out the protobuf message
and below is my code in python to receive the protobuf message...

class MyUDPHandler(SocketServer.BaseRequestHandler):

def handle(self):
    data = self.request[0].strip()
    socket = self.request[1]
    print "{} wrote:".format(self.client_address[0])
    print data
    socket.sendto(data.upper(), self.client_address)

if name == 'main':

 HOST, PORT = "localhost", 9994
 server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
 server.serve_forever()

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

3 participants