My BinaryJS webclient receives binary data, how do i convert it back to the text input which the server sent?
The Server (with NodeJS), which creates a Websocket, uses BinaryJS because it gets the data to transmit as a stream, and i wanted to forward that data to the client using Websocket streaming.
The web client looks the following:
<html>
<head>
<script src="http://cdn.binaryjs.com/0/binary.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script src="text-encoding/lib/encoding.js"></script>
<script src="decode.js"></script>
<script>
// Connect to Binary.js server
var client = new BinaryClient('ws://localhost:9000');
// Received new stream from server!
client.on('stream', function(stream, meta){
// Buffer for parts
// Got new data
stream.on('data', function(data){
$( ".inner" ).append(data+"<br />");
});
stream.on('end', function(){
});
});
</script>
</head>
<body>
<div class="inner">Output:</div>
</body>
</html>
Sadly my output is not the input text but only something like this:
[object ArrayBuffer]
Edit: Found a possible solution:
As the data is UTF-8 encoded, i could simply decode it using a function someone posted in another question:
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}