1 |
|
---|
2 | # socket.io-parser
|
---|
3 |
|
---|
4 | [![Build Status](https://github.com/socketio/socket.io-parser/workflows/CI/badge.svg)](https://github.com/socketio/socket.io-parser/actions)
|
---|
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 `5`
|
---|
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 | Compatibility table:
|
---|
13 |
|
---|
14 | | Parser version | Socket.IO server version | Protocol revision |
|
---|
15 | |----------------| ------------------------ | ----------------- |
|
---|
16 | | 3.x | 1.x / 2.x | 4 |
|
---|
17 | | 4.x | 3.x | 5 |
|
---|
18 |
|
---|
19 |
|
---|
20 | ## Parser API
|
---|
21 |
|
---|
22 | socket.io-parser is the reference implementation of socket.io-protocol. Read
|
---|
23 | the full API here:
|
---|
24 | [socket.io-protocol](https://github.com/learnboost/socket.io-protocol).
|
---|
25 |
|
---|
26 | ## Example Usage
|
---|
27 |
|
---|
28 | ### Encoding and decoding a packet
|
---|
29 |
|
---|
30 | ```js
|
---|
31 | var parser = require('socket.io-parser');
|
---|
32 | var encoder = new parser.Encoder();
|
---|
33 | var packet = {
|
---|
34 | type: parser.EVENT,
|
---|
35 | data: 'test-packet',
|
---|
36 | id: 13
|
---|
37 | };
|
---|
38 | encoder.encode(packet, function(encodedPackets) {
|
---|
39 | var decoder = new parser.Decoder();
|
---|
40 | decoder.on('decoded', function(decodedPacket) {
|
---|
41 | // decodedPacket.type == parser.EVENT
|
---|
42 | // decodedPacket.data == 'test-packet'
|
---|
43 | // decodedPacket.id == 13
|
---|
44 | });
|
---|
45 |
|
---|
46 | for (var i = 0; i < encodedPackets.length; i++) {
|
---|
47 | decoder.add(encodedPackets[i]);
|
---|
48 | }
|
---|
49 | });
|
---|
50 | ```
|
---|
51 |
|
---|
52 | ### Encoding and decoding a packet with binary data
|
---|
53 |
|
---|
54 | ```js
|
---|
55 | var parser = require('socket.io-parser');
|
---|
56 | var encoder = new parser.Encoder();
|
---|
57 | var packet = {
|
---|
58 | type: parser.BINARY_EVENT,
|
---|
59 | data: {i: new Buffer(1234), j: new Blob([new ArrayBuffer(2)])},
|
---|
60 | id: 15
|
---|
61 | };
|
---|
62 | encoder.encode(packet, function(encodedPackets) {
|
---|
63 | var decoder = new parser.Decoder();
|
---|
64 | decoder.on('decoded', function(decodedPacket) {
|
---|
65 | // decodedPacket.type == parser.BINARY_EVENT
|
---|
66 | // Buffer.isBuffer(decodedPacket.data.i) == true
|
---|
67 | // Buffer.isBuffer(decodedPacket.data.j) == true
|
---|
68 | // decodedPacket.id == 15
|
---|
69 | });
|
---|
70 |
|
---|
71 | for (var i = 0; i < encodedPackets.length; i++) {
|
---|
72 | decoder.add(encodedPackets[i]);
|
---|
73 | }
|
---|
74 | });
|
---|
75 | ```
|
---|
76 | See the test suite for more examples of how socket.io-parser is used.
|
---|
77 |
|
---|
78 |
|
---|
79 | ## License
|
---|
80 |
|
---|
81 | MIT
|
---|