Deserializing encoded UTF8 Byte [] in javascript in a browser or node.js application?

I have an event class in C #:

public class AreaInterventoCreata{
    //public properties
}
var message= new AreaInterventoCreata();

I create an instance of this side of the class server. My goal is to convey this creation to customers who have subscribed to this type of event.

So I pass it on to my RabbitMQ broker next time byte[]:

var responseBody = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message))
responseChannel.BasicPublish(
                             "",                 // exchange 
                             properties.ReplyTo, // routingKey
                             responseProperties, // basicProperties 
                             responseBody);      // body

The node.js server subscribed to this message:

q.subscribe(function (message) { 
    for (var i = 0; i < sockets.length; i++) {
        console.log('_sockets[' + i + '] emitted');
        sockets[i].emit(event, message);
    }
});

This node.js itself received a socket.io connection coming from browsers, and therefore can push for these sockets, for example, these browsers:

sockets[i].emit(event, message);

Finally, I get this message in my browser:

var socket = io.connect('http://localhost:8091');
socket.on('Events_AreaIntervento_AreaInterventoCreata:Events', function (data) {
    var json = Utf8.decode(data);
});

When I check the data through firebug, this object data with an array of numbers determines how data.data.

I assumed that this array of number is an array of byte i, indicated at the beginning of the process. Am I mistaken in this assumption?

  • , - javascript? ( !)
  • json node.js? ? - ?

ok ( , )

var Utf8 = {
    // public method for url decoding
    decodeArray: function (utfArray) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utfArray.length) {

            c = utfArray[i];

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utfArray[i + 1];
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utfArray[i + 1];
                c3 = utfArray[i + 2];
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;
    }
}

<script>
    var socket = io.connect('http://localhost:8091');
    socket.on('Events_AreaIntervento_AreaInterventoCreata:Events', function (msg) {
        var jsonString = Utf8.decodeArray(msg.data);
        alert(jsonString);
    });
</script>

, node.js, node.js?

+3
1

RabbitMQ, , - , JSON .NET RabbitMQ. , "responseProperties" , UTF-8 JSON .

0

All Articles