twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

Readme.md (2093B)


      1 
      2 # socket.io-parser
      3 
      4 [![Build Status](https://secure.travis-ci.org/socketio/socket.io-parser.svg?branch=master)](http://travis-ci.org/socketio/socket.io-parser)
      5 [![NPM version](https://badge.fury.io/js/socket.io-parser.svg)](http://badge.fury.io/js/socket.io-parser)
      6 
      7 A socket.io encoder and decoder written in JavaScript complying with version `3`
      8 of [socket.io-protocol](https://github.com/socketio/socket.io-protocol).
      9 Used by [socket.io](https://github.com/automattic/socket.io) and
     10 [socket.io-client](https://github.com/automattic/socket.io-client).
     11 
     12 ## Parser API
     13 
     14   socket.io-parser is the reference implementation of socket.io-protocol. Read
     15   the full API here:
     16   [socket.io-protocol](https://github.com/learnboost/socket.io-protocol).
     17 
     18 ## Example Usage
     19 
     20 ### Encoding and decoding a packet
     21 
     22 ```js
     23 var parser = require('socket.io-parser');
     24 var encoder = new parser.Encoder();
     25 var packet = {
     26   type: parser.EVENT,
     27   data: 'test-packet',
     28   id: 13
     29 };
     30 encoder.encode(packet, function(encodedPackets) {
     31   var decoder = new parser.Decoder();
     32   decoder.on('decoded', function(decodedPacket) {
     33     // decodedPacket.type == parser.EVENT
     34     // decodedPacket.data == 'test-packet'
     35     // decodedPacket.id == 13
     36   });
     37 
     38   for (var i = 0; i < encodedPackets.length; i++) {
     39     decoder.add(encodedPackets[i]);
     40   }
     41 });
     42 ```
     43 
     44 ### Encoding and decoding a packet with binary data
     45 
     46 ```js
     47 var parser = require('socket.io-parser');
     48 var encoder = new parser.Encoder();
     49 var packet = {
     50   type: parser.BINARY_EVENT,
     51   data: {i: new Buffer(1234), j: new Blob([new ArrayBuffer(2)])}
     52   id: 15
     53 };
     54 encoder.encode(packet, function(encodedPackets) {
     55   var decoder = new parser.Decoder();
     56   decoder.on('decoded', function(decodedPacket) {
     57     // decodedPacket.type == parser.BINARY_EVENT
     58     // Buffer.isBuffer(decodedPacket.data.i) == true
     59     // Buffer.isBuffer(decodedPacket.data.j) == true
     60     // decodedPacket.id == 15
     61   });
     62 
     63   for (var i = 0; i < encodedPackets.length; i++) {
     64     decoder.add(encodedPackets[i]);
     65   }
     66 });
     67 ```
     68 See the test suite for more examples of how socket.io-parser is used.
     69 
     70 
     71 ## License
     72 
     73 MIT