forked from socketio/socket.io
-
Notifications
You must be signed in to change notification settings - Fork 1
Migrating to 1.0
Kevin Roark edited this page Apr 7, 2014
·
17 revisions
This will explain the main differences between 0.9x and 1.0, and should help ease the migration process. It's gonna rule!!
This is only relevant for updating things like socket.io implementations in other languages, custom socket.io clients, etc.
Parsing is now class based and asynchronous. Instead of returning a single encoded string, encode calls callback with an array of encodings as the only argument. Each encoding should be written to the transport in order. This is more flexible and makes binary data transport work. Here's an example:
var encoding = parser.encode(packet);
console.log(encoding); // fully encoded packetvs.
var encoder = new parser.Encoder();
encoder.encode(packet, function(encodings) {
for (var i = 0; i < encodings.length; i++) {
console.log(encodings[i]); // encoded parts of the packet
}
});Decoding takes things a step further and is event-based. This is done because some objects (binary-containing) are both encoded and decoded in multiple parts. This example should help:
var packet = parser.decode(decoding);
console.log(packet); // formed socket.io packet to handlevs.
var decoder = new parser.Decoder();
decoder.on('decoded', function(packet) {
console.log(packet); // formed socket.io packet to handle
});
decoder.add(encodings[0]); // say encodings is array of two encodings received from transport
decoder.add(encodings[1]); // after adding the last element, 'decoded' is emitted from decoder