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.

166 lines
5.1 KiB

  1. # socket.io-client
  2. [![Build Status](https://secure.travis-ci.org/Automattic/socket.io-client.svg)](http://travis-ci.org/Automattic/socket.io-client)
  3. [![NPM version](https://badge.fury.io/js/socket.io-client.svg)](http://badge.fury.io/js/socket.io-client)
  4. ![Downloads](http://img.shields.io/npm/dm/socket.io-client.svg)
  5. ## How to use
  6. A standalone build of `socket.io-client` is exposed automatically by the
  7. socket.io server as `/socket.io/socket.io.js`. Alternatively you can
  8. serve the file `socket.io.js` found at the root of this repository.
  9. ```html
  10. <script src="/socket.io/socket.io.js"></script>
  11. <script>
  12. var socket = io('http://localhost');
  13. socket.on('connect', function(){
  14. socket.on('event', function(data){});
  15. socket.on('disconnect', function(){});
  16. });
  17. </script>
  18. ```
  19. Socket.IO is compatible with [browserify](http://browserify.org/).
  20. ### Node.JS (server-side usage)
  21. Add `socket.io-client` to your `package.json` and then:
  22. ```js
  23. var socket = require('socket.io-client')('http://localhost');
  24. socket.on('connect', function(){
  25. socket.on('event', function(data){});
  26. socket.on('disconnect', function(){});
  27. });
  28. ```
  29. ## API
  30. ### IO(url:String, opts:Object):Socket
  31. Exposed as the `io` namespace in the standalone build, or the result
  32. of calling `require('socket.io-client')`.
  33. When called, it creates a new `Manager` for the given URL, and attempts
  34. to reuse an existing `Manager` for subsequent calls, unless the
  35. `multiplex` option is passed with `false`.
  36. The rest of the options are passed to the `Manager` constructor (see below
  37. for details).
  38. A `Socket` instance is returned for the namespace specified by the
  39. pathname in the URL, defaulting to `/`. For example, if the `url` is
  40. `http://localhost/users`, a transport connection will be established to
  41. `http://localhost` and a Socket.IO connection will be established to
  42. `/users`.
  43. ### IO#protocol
  44. Socket.io protocol revision number this client works with.
  45. ### IO#Socket
  46. Reference to the `Socket` constructor.
  47. ### IO#Manager
  48. Reference to the `Manager` constructor.
  49. ### IO#Emitter
  50. Reference to the `Emitter` constructor.
  51. ### Manager(url:String, opts:Object)
  52. A `Manager` represents a connection to a given Socket.IO server. One or
  53. more `Socket` instances are associated with the manager. The manager
  54. can be accessed through the `io` property of each `Socket` instance.
  55. The `opts` are also passed to `engine.io` upon initialization of the
  56. underlying `Socket`.
  57. Options:
  58. - `reconnection` whether to reconnect automatically (`true`)
  59. - `reconnectionDelay` how long to wait before attempting a new
  60. reconnection (`1000`)
  61. - `reconnectionDelayMax` maximum amount of time to wait between
  62. reconnections (`5000`). Each attempt increases the reconnection by
  63. the amount specified by `reconnectionDelay`.
  64. - `timeout` connection timeout before a `connect_error`
  65. and `connect_timeout` events are emitted (`20000`)
  66. - `autoConnect` by setting this false, you have to call `manager.open`
  67. whenever you decide it's appropriate
  68. #### Events
  69. - `connect`. Fired upon a successful connection.
  70. - `connect_error`. Fired upon a connection error.
  71. Parameters:
  72. - `Object` error object
  73. - `connect_timeout`. Fired upon a connection timeout.
  74. - `reconnect`. Fired upon a successful reconnection.
  75. Parameters:
  76. - `Number` reconnection attempt number
  77. - `reconnect_attempt`. Fired upon an attempt to reconnect.
  78. - `reconnecting`. Fired upon an attempt to reconnect.
  79. Parameters:
  80. - `Number` reconnection attempt number
  81. - `reconnect_error`. Fired upon a reconnection attempt error.
  82. Parameters:
  83. - `Object` error object
  84. - `reconnect_failed`. Fired when couldn't reconnect within `reconnectionAttempts`
  85. The events above are also emitted on the individual sockets that
  86. reconnect that depend on this `Manager`.
  87. ### Manager#reconnection(v:Boolean):Manager
  88. Sets the `reconnection` option, or returns it if no parameters
  89. are passed.
  90. ### Manager#reconnectionAttempts(v:Boolean):Manager
  91. Sets the `reconnectionAttempts` option, or returns it if no parameters
  92. are passed.
  93. ### Manager#reconnectionDelay(v:Boolean):Manager
  94. Sets the `reconectionDelay` option, or returns it if no parameters
  95. are passed.
  96. ### Manager#reconnectionDelayMax(v:Boolean):Manager
  97. Sets the `reconectionDelayMax` option, or returns it if no parameters
  98. are passed.
  99. ### Manager#timeout(v:Boolean):Manager
  100. Sets the `timeout` option, or returns it if no parameters
  101. are passed.
  102. ### Socket
  103. #### Events
  104. - `connect`. Fired upon connecting.
  105. - `error`. Fired upon a connection error
  106. Parameters:
  107. - `Object` error data
  108. - `disconnect`. Fired upon a disconnection.
  109. - `reconnect`. Fired upon a successful reconnection.
  110. Parameters:
  111. - `Number` reconnection attempt number
  112. - `reconnect_attempt`. Fired upon an attempt to reconnect.
  113. - `reconnecting`. Fired upon an attempt to reconnect.
  114. Parameters:
  115. - `Number` reconnection attempt number
  116. - `reconnect_error`. Fired upon a reconnection attempt error.
  117. Parameters:
  118. - `Object` error object
  119. - `reconnect_failed`. Fired when couldn't reconnect within `reconnectionAttempts`
  120. ## License
  121. MIT