You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.2 KiB

7 years ago
  1. # engine.io-parser
  2. [![Build Status](https://secure.travis-ci.org/socketio/engine.io-parser.svg)](http://travis-ci.org/socketio/engine.io-parser)
  3. [![NPM version](https://badge.fury.io/js/engine.io-parser.svg)](http://badge.fury.io/js/engine.io-parser)
  4. This is the JavaScript parser for the engine.io protocol encoding,
  5. shared by both
  6. [engine.io-client](https://github.com/socketio/engine.io-client) and
  7. [engine.io](https://github.com/socketio/engine.io).
  8. ## How to use
  9. ### Standalone
  10. The parser can encode/decode packets, payloads, and payloads as binary
  11. with the following methods: `encodePacket`, `decodePacket`, `encodePayload`,
  12. `decodePayload`, `encodePayloadAsBinary`, `decodePayloadAsBinary`.
  13. The browser-side parser also includes `encodePayloadAsArrayBuffer` and `encodePayloadAsBlob`.
  14. Example:
  15. ```js
  16. var parser = require('engine.io-parser');
  17. var data = new Buffer(5);
  18. for (var i = 0; i < data.length; i++) { data[i] = i; }
  19. parser.encodePacket({ type: 'message', data: data }, function(encoded) {
  20. var decodedData = parser.decodePacket(encoded); // { type: 'message', data: data }
  21. });
  22. ```
  23. ### With browserify
  24. Engine.IO Parser is a commonjs module, which means you can include it by using
  25. `require` on the browser and package using [browserify](http://browserify.org/):
  26. 1. install the parser package
  27. ```shell
  28. npm install engine.io-parser
  29. ```
  30. 1. write your app code
  31. ```js
  32. var parser = require('engine.io-parser');
  33. var testBuffer = new Int8Array(10);
  34. for (var i = 0; i < testBuffer.length; i++) testBuffer[i] = i;
  35. var packets = [{ type: 'message', data: testBuffer.buffer }, { type: 'message', data: 'hello' }];
  36. parser.encodePayload(packets, function(encoded) {
  37. parser.decodePayload(encoded,
  38. function(packet, index, total) {
  39. var isLast = index + 1 == total;
  40. if (!isLast) {
  41. var buffer = new Int8Array(packet.data); // testBuffer
  42. } else {
  43. var message = packet.data; // 'hello'
  44. }
  45. });
  46. });
  47. ```
  48. 1. build your app bundle
  49. ```bash
  50. $ browserify app.js > bundle.js
  51. ```
  52. 1. include on your page
  53. ```html
  54. <script src="/path/to/bundle.js"></script>
  55. ```
  56. ## Features
  57. - Runs on browser and node.js seamlessly
  58. - Runs inside HTML5 WebWorker
  59. - Can encode and decode packets
  60. - Encodes from/to ArrayBuffer or Blob when in browser, and Buffer or ArrayBuffer in Node
  61. ## API
  62. Note: `cb(type)` means the type is a callback function that contains a parameter of type `type` when called.
  63. ### Node
  64. - `encodePacket`
  65. - Encodes a packet.
  66. - **Parameters**
  67. - `Object`: the packet to encode, has `type` and `data`.
  68. - `data`: can be a `String`, `Number`, `Buffer`, `ArrayBuffer`
  69. - `Boolean`: optional, binary support
  70. - `Function`: callback, returns the encoded packet (`cb(String)`)
  71. - `decodePacket`
  72. - Decodes a packet. Data also available as an ArrayBuffer if requested.
  73. - Returns data as `String` or (`Blob` on browser, `ArrayBuffer` on Node)
  74. - **Parameters**
  75. - `String` | `ArrayBuffer`: the packet to decode, has `type` and `data`
  76. - `String`: optional, the binary type
  77. - `encodeBase64Packet`
  78. - Encodes a packet with binary data in a base64 string (`String`)
  79. - **Parameters**
  80. - `Object`: the packet to encode, has `type` and `data`
  81. - `Function`: callback, returns the base64 encoded message (`cb(String)`)
  82. - `decodeBase64Packet`
  83. - Decodes a packet encoded in a base64 string.
  84. - **Parameters**
  85. - `String`: the base64 encoded message
  86. - `String`: optional, the binary type
  87. - `encodePayload`
  88. - Encodes multiple messages (payload).
  89. - If any contents are binary, they will be encoded as base64 strings. Base64
  90. encoded strings are marked with a b before the length specifier
  91. - **Parameters**
  92. - `Array`: an array of packets
  93. - `Boolean`: optional, binary support
  94. - `Function`: callback, returns the encoded payload (`cb(String)`)
  95. - `decodePayload`
  96. - Decodes data when a payload is maybe expected. Possible binary contents are
  97. decoded from their base64 representation.
  98. - **Parameters**
  99. - `String`: the payload
  100. - `String`: optional, the binary type
  101. - `Function`: callback, returns (cb(`Object`: packet, `Number`:packet index, `Number`:packet total))
  102. - `encodePayloadAsBinary`
  103. - Encodes multiple messages (payload) as binary.
  104. - **Parameters**
  105. - `Array`: an array of packets
  106. - `Function`: callback, returns the encoded payload (`cb(Buffer)`)
  107. - `decodePayloadAsBinary`
  108. - Decodes data when a payload is maybe expected. Strings are decoded by
  109. interpreting each byte as a key code for entries marked to start with 0. See
  110. description of encodePayloadAsBinary.
  111. - **Parameters**
  112. - `Buffer`: the buffer
  113. - `String`: optional, the binary type
  114. - `Function`: callback, returns the decoded packet (`cb(Object)`)
  115. ### Browser
  116. - `encodePayloadAsArrayBuffer`
  117. - Encodes multiple messages (payload) as binary.
  118. - **Parameters**
  119. - `Array`: an array of packets
  120. - `Function`: callback, returns the encoded payload (`cb(ArrayBuffer)`)
  121. - `encodePayloadAsBlob`
  122. - Encodes multiple messages (payload) as blob.
  123. - **Parameters**
  124. - `Array`: an array of packets
  125. - `Function`: callback, returns the encoded payload (`cb(Blob)`)
  126. ## Tests
  127. Standalone tests can be run with `make test` which will run both node.js and browser tests.
  128. Browser tests are run using [zuul](https://github.com/defunctzombie/zuul).
  129. (You must have zuul setup with a saucelabs account.)
  130. You can run the tests locally using the following command:
  131. ```
  132. ./node_modules/.bin/zuul --local 8080 -- test/index.js
  133. ```
  134. ## Support
  135. The support channels for `engine.io-parser` are the same as `socket.io`:
  136. - irc.freenode.net **#socket.io**
  137. - [Google Groups](http://groups.google.com/group/socket_io)
  138. - [Website](http://socket.io)
  139. ## Development
  140. To contribute patches, run tests or benchmarks, make sure to clone the
  141. repository:
  142. ```bash
  143. git clone git://github.com/LearnBoost/engine.io-parser.git
  144. ```
  145. Then:
  146. ```bash
  147. cd engine.io-parser
  148. npm install
  149. ```
  150. See the `Tests` section above for how to run tests before submitting any patches.
  151. ## License
  152. MIT