linx-simulator2/node_modules/socket.io-parser
2019-11-22 09:43:37 +00:00
..
node_modules Added custom log name 2019-11-22 09:43:37 +00:00
binary.js Simulator first commit 2019-09-18 11:11:16 +03:00
index.js Simulator first commit 2019-09-18 11:11:16 +03:00
is-buffer.js Simulator first commit 2019-09-18 11:11:16 +03:00
LICENSE Simulator first commit 2019-09-18 11:11:16 +03:00
package.json Added custom log name 2019-11-22 09:43:37 +00:00
Readme.md Simulator first commit 2019-09-18 11:11:16 +03:00

socket.io-parser

Build Status NPM version

A socket.io encoder and decoder written in JavaScript complying with version 3 of socket.io-protocol. Used by socket.io and socket.io-client.

Parser API

socket.io-parser is the reference implementation of socket.io-protocol. Read the full API here: socket.io-protocol.

Example Usage

Encoding and decoding a packet

var parser = require('socket.io-parser');
var encoder = new parser.Encoder();
var packet = {
  type: parser.EVENT,
  data: 'test-packet',
  id: 13
};
encoder.encode(packet, function(encodedPackets) {
  var decoder = new parser.Decoder();
  decoder.on('decoded', function(decodedPacket) {
    // decodedPacket.type == parser.EVENT
    // decodedPacket.data == 'test-packet'
    // decodedPacket.id == 13
  });

  for (var i = 0; i < encodedPackets.length; i++) {
    decoder.add(encodedPackets[i]);
  }
});

Encoding and decoding a packet with binary data

var parser = require('socket.io-parser');
var encoder = new parser.Encoder();
var packet = {
  type: parser.BINARY_EVENT,
  data: {i: new Buffer(1234), j: new Blob([new ArrayBuffer(2)])}
  id: 15
};
encoder.encode(packet, function(encodedPackets) {
  var decoder = new parser.Decoder();
  decoder.on('decoded', function(decodedPacket) {
    // decodedPacket.type == parser.BINARY_EVENT
    // Buffer.isBuffer(decodedPacket.data.i) == true
    // Buffer.isBuffer(decodedPacket.data.j) == true
    // decodedPacket.id == 15
  });

  for (var i = 0; i < encodedPackets.length; i++) {
    decoder.add(encodedPackets[i]);
  }
});

See the test suite for more examples of how socket.io-parser is used.

License

MIT