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.

243 lines
7.8 KiB

  1. # Engine.IO client
  2. [![Build Status](https://secure.travis-ci.org/Automattic/engine.io-client.png)](http://travis-ci.org/Automattic/engine.io-client)
  3. [![NPM version](https://badge.fury.io/js/engine.io-client.png)](http://badge.fury.io/js/engine.io-client)
  4. This is the client for [Engine.IO](http://github.com/automattic/engine.io),
  5. the implementation of transport-based cross-browser/cross-device
  6. bi-directional communication layer for [Socket.IO](http://github.com/automattic/socket.io).
  7. ## How to use
  8. ### Standalone
  9. You can find an `engine.io.js` file in this repository, which is a
  10. standalone build you can use as follows:
  11. ```html
  12. <script src="/path/to/engine.io.js"></script>
  13. <script>
  14. // eio = Socket
  15. var socket = eio('ws://localhost');
  16. socket.on('open', function(){
  17. socket.on('message', function(data){});
  18. socket.on('close', function(){});
  19. });
  20. </script>
  21. ```
  22. ### With browserify
  23. Engine.IO is a commonjs module, which means you can include it by using
  24. `require` on the browser and package using [browserify](http://browserify.org/):
  25. 1. install the client package
  26. ```bash
  27. $ npm install engine.io-client
  28. ```
  29. 1. write your app code
  30. ```js
  31. var socket = require('engine.io-client')('ws://localhost');
  32. socket.on('open', function(){
  33. socket.on('message', function(data){});
  34. socket.on('close', function(){});
  35. });
  36. ```
  37. 1. build your app bundle
  38. ```bash
  39. $ browserify app.js > bundle.js
  40. ```
  41. 1. include on your page
  42. ```html
  43. <script src="/path/to/bundle.js"></script>
  44. ```
  45. ### Sending and receiving binary
  46. ```html
  47. <script src="/path/to/engine.io.js"></script>
  48. <script>
  49. var socket = new eio.Socket('ws://localhost/');
  50. socket.binaryType = 'blob';
  51. socket.on('open', function () {
  52. socket.send(new Int8Array(5));
  53. socket.on('message', function(blob){});
  54. socket.on('close', function(){ });
  55. });
  56. </script>
  57. ```
  58. ### Node.JS
  59. Add `engine.io-client` to your `package.json` and then:
  60. ```js
  61. var socket = require('engine.io-client')('ws://localhost');
  62. socket.on('open', function(){
  63. socket.on('message', function(data){});
  64. socket.on('close', function(){});
  65. });
  66. ```
  67. ## Features
  68. - Lightweight
  69. - Runs on browser and node.js seamlessly
  70. - Transports are independent of `Engine`
  71. - Easy to debug
  72. - Easy to unit test
  73. - Runs inside HTML5 WebWorker
  74. - Can send and receive binary data
  75. - Receives as ArrayBuffer or Blob when in browser, and Buffer or ArrayBuffer
  76. in Node
  77. - When XHR2 or WebSockets are used, binary is emitted directly. Otherwise
  78. binary is encoded into base64 strings, and decoded when binary types are
  79. supported.
  80. - With browsers that don't support ArrayBuffer, an object { base64: true,
  81. data: dataAsBase64String } is emitted on the `message` event.
  82. ## API
  83. ### Socket
  84. The client class. Mixes in [Emitter](http://github.com/component/emitter).
  85. Exposed as `eio` in the browser standalone build.
  86. #### Properties
  87. - `protocol` _(Number)_: protocol revision number
  88. - `binaryType` _(String)_ : can be set to 'arraybuffer' or 'blob' in browsers,
  89. and `buffer` or `arraybuffer` in Node. Blob is only used in browser if it's
  90. supported.
  91. #### Events
  92. - `open`
  93. - Fired upon successful connection.
  94. - `message`
  95. - Fired when data is received from the server.
  96. - **Arguments**
  97. - `String` | `ArrayBuffer`: utf-8 encoded data or ArrayBuffer containing
  98. binary data
  99. - `close`
  100. - Fired upon disconnection. In compliance with the WebSocket API spec, this event may be
  101. fired even if the `open` event does not occur (i.e. due to connection error or `close()`).
  102. - `error`
  103. - Fired when an error occurs.
  104. - `flush`
  105. - Fired upon completing a buffer flush
  106. - `drain`
  107. - Fired after `drain` event of transport if writeBuffer is empty
  108. - `upgradeError`
  109. - Fired if an error occurs with a transport we're trying to upgrade to.
  110. - `upgrade`
  111. - Fired upon upgrade success, after the new transport is set
  112. #### Methods
  113. - **constructor**
  114. - Initializes the client
  115. - **Parameters**
  116. - `String` uri
  117. - `Object`: optional, options object
  118. - **Options**
  119. - `agent` (`http.Agent`): `http.Agent` to use, defaults to `false` (NodeJS only)
  120. - `upgrade` (`Boolean`): defaults to true, whether the client should try
  121. to upgrade the transport from long-polling to something better.
  122. - `forceJSONP` (`Boolean`): forces JSONP for polling transport.
  123. - `jsonp` (`Boolean`): determines whether to use JSONP when
  124. necessary for polling. If disabled (by settings to false) an error will
  125. be emitted (saying "No transports available") if no other transports
  126. are available. If another transport is available for opening a
  127. connection (e.g. WebSocket) that transport
  128. will be used instead.
  129. - `forceBase64` (`Boolean`): forces base 64 encoding for polling transport even when XHR2 responseType is available and WebSocket even if the used standard supports binary.
  130. - `enablesXDR` (`Boolean`): enables XDomainRequest for IE8 to avoid loading bar flashing with click sound. default to `false` because XDomainRequest has a flaw of not sending cookie.
  131. - `timestampRequests` (`Boolean`): whether to add the timestamp with
  132. each transport request. Note: this is ignored if the browser is
  133. IE or Android, in which case requests are always stamped (`false`)
  134. - `timestampParam` (`String`): timestamp parameter (`t`)
  135. - `policyPort` (`Number`): port the policy server listens on (`843`)
  136. - `path` (`String`): path to connect to, default is `/engine.io`
  137. - `transports` (`Array`): a list of transports to try (in order).
  138. Defaults to `['polling', 'websocket']`. `Engine`
  139. always attempts to connect directly with the first one, provided the
  140. feature detection test for it passes.
  141. - `rememberUpgrade` (`Boolean`): defaults to false.
  142. If true and if the previous websocket connection to the server succeeded,
  143. the connection attempt will bypass the normal upgrade process and will initially
  144. try websocket. A connection attempt following a transport error will use the
  145. normal upgrade process. It is recommended you turn this on only when using
  146. SSL/TLS connections, or if you know that your network does not block websockets.
  147. - `send`
  148. - Sends a message to the server
  149. - **Parameters**
  150. - `String` | `ArrayBuffer` | `ArrayBufferView` | `Blob`: data to send
  151. - `Function`: optional, callback upon `drain`
  152. - `close`
  153. - Disconnects the client.
  154. ### Transport
  155. The transport class. Private. _Inherits from EventEmitter_.
  156. #### Events
  157. - `poll`: emitted by polling transports upon starting a new request
  158. - `pollComplete`: emitted by polling transports upon completing a request
  159. - `drain`: emitted by polling transports upon a buffer drain
  160. ## Tests
  161. `engine.io-client` is used to test
  162. [engine](http://github.com/automattic/engine.io). Running the `engine.io`
  163. test suite ensures the client works and vice-versa.
  164. Browser tests are run using [zuul](https://github.com/defunctzombie/zuul). You can
  165. run the tests locally using the following command.
  166. ```
  167. ./node_modules/.bin/zuul --local 8080 -- test/index.js
  168. ```
  169. Additionally, `engine.io-client` has a standalone test suite you can run
  170. with `make test` which will run node.js and browser tests. You must have zuul setup with
  171. a saucelabs account.
  172. ## Support
  173. The support channels for `engine.io-client` are the same as `socket.io`:
  174. - irc.freenode.net **#socket.io**
  175. - [Google Groups](http://groups.google.com/group/socket_io)
  176. - [Website](http://socket.io)
  177. ## Development
  178. To contribute patches, run tests or benchmarks, make sure to clone the
  179. repository:
  180. ```bash
  181. git clone git://github.com/automattic/engine.io-client.git
  182. ```
  183. Then:
  184. ```bash
  185. cd engine.io-client
  186. npm install
  187. ```
  188. See the `Tests` section above for how to run tests before submitting any patches.
  189. ## License
  190. MIT - Copyright (c) 2014 Automattic, Inc.