source: trip-planner-front/node_modules/socket.io-parser/Readme.md@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
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
7A socket.io encoder and decoder written in JavaScript complying with version `5`
8of [socket.io-protocol](https://github.com/socketio/socket.io-protocol).
9Used by [socket.io](https://github.com/automattic/socket.io) and
10[socket.io-client](https://github.com/automattic/socket.io-client).
11
12Compatibility 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
31var parser = require('socket.io-parser');
32var encoder = new parser.Encoder();
33var packet = {
34 type: parser.EVENT,
35 data: 'test-packet',
36 id: 13
37};
38encoder.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
55var parser = require('socket.io-parser');
56var encoder = new parser.Encoder();
57var packet = {
58 type: parser.BINARY_EVENT,
59 data: {i: new Buffer(1234), j: new Blob([new ArrayBuffer(2)])},
60 id: 15
61};
62encoder.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```
76See the test suite for more examples of how socket.io-parser is used.
77
78
79## License
80
81MIT
Note: See TracBrowser for help on using the repository browser.