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.

181 lines
5.0 KiB

  1. # ws
  2. ## Class: ws.Server
  3. This class is a WebSocket server. It is an `EventEmitter`.
  4. ### new ws.Server([options], [callback])
  5. * `options` Object
  6. * `host` String
  7. * `port` Number
  8. * `server` http.Server
  9. * `verifyClient` Function
  10. * `path` String
  11. * `noServer` Boolean
  12. * `disableHixie` Boolean
  13. * `clientTracking` Boolean
  14. * `callback` Function
  15. Construct a new server object.
  16. Either `port` or `server` must be provided, otherwise you might enable
  17. `noServer` if you want to pass the requests directly. Please note that the
  18. `callback` is only used when you supply the a `port` number in the options.
  19. ### server.close([code], [data])
  20. Close the server and terminate all clients
  21. ### server.handleUpgrade(request, socket, upgradeHead, callback)
  22. Handles a HTTP Upgrade request. `request` is an instance of `http.ServerRequest`, `socket` is an instance of `net.Socket`.
  23. When the Upgrade was successfully, the `callback` will be called with a `ws.WebSocket` object as parameter.
  24. ### Event: 'error'
  25. `function (error) { }`
  26. If the underlying server emits an error, it will be forwarded here.
  27. ### Event: 'headers'
  28. `function (headers) { }`
  29. Emitted with the object of HTTP headers that are going to be written to the `Stream` as part of the handshake.
  30. ### Event: 'connection'
  31. `function (socket) { }`
  32. When a new WebSocket connection is established. `socket` is an object of type `ws.WebSocket`.
  33. ## Class: ws.WebSocket
  34. This class represents a WebSocket connection. It is an `EventEmitter`.
  35. ### new ws.WebSocket(address, [options])
  36. * `address` String|Array
  37. * `options` Object
  38. * `protocol` String
  39. * `agent` Agent
  40. * `headers` Object
  41. * `protocolVersion` Number|String
  42. -- the following only apply if `address` is a String
  43. * `host` String
  44. * `origin` String
  45. * `pfx` String|Buffer
  46. * `key` String|Buffer
  47. * `passphrase` String
  48. * `cert` String|Buffer
  49. * `ca` Array
  50. * `ciphers` String
  51. * `rejectUnauthorized` Boolean
  52. Instantiating with an `address` creates a new WebSocket client object. If `address` is an Array (request, socket, rest), it is instantiated as a Server client (e.g. called from the `ws.Server`).
  53. ### websocket.bytesReceived
  54. Received bytes count.
  55. ### websocket.readyState
  56. Possible states are `WebSocket.CONNECTING`, `WebSocket.OPEN`, `WebSocket.CLOSING`, `WebSocket.CLOSED`.
  57. ### websocket.protocolVersion
  58. The WebSocket protocol version used for this connection, `8`, `13` or `hixie-76` (the latter only for server clients).
  59. ### websocket.url
  60. The URL of the WebSocket server (only for clients)
  61. ### websocket.supports
  62. Describes the feature of the used protocol version. E.g. `supports.binary` is a boolean that describes if the connection supports binary messages.
  63. ### websocket.close([code], [data])
  64. Gracefully closes the connection, after sending a description message
  65. ### websocket.pause()
  66. Pause the client stream
  67. ### websocket.ping([data], [options], [dontFailWhenClosed])
  68. Sends a ping. `data` is sent, `options` is an object with members `mask` and `binary`. `dontFailWhenClosed` indicates whether or not to throw if the connection isnt open.
  69. ### websocket.pong([data], [options], [dontFailWhenClosed])
  70. Sends a pong. `data` is sent, `options` is an object with members `mask` and `binary`. `dontFailWhenClosed` indicates whether or not to throw if the connection isnt open.
  71. ### websocket.resume()
  72. Resume the client stream
  73. ### websocket.send(data, [options], [callback])
  74. Sends `data` through the connection. `options` can be an object with members `mask` and `binary`. The optional `callback` is executed after the send completes.
  75. ### websocket.stream([options], callback)
  76. Streams data through calls to a user supplied function. `options` can be an object with members `mask` and `binary`. `callback` is executed on successive ticks of which send is `function (data, final)`.
  77. ### websocket.terminate()
  78. Immediately shuts down the connection
  79. ### websocket.onopen
  80. ### websocket.onerror
  81. ### websocket.onclose
  82. ### websocket.onmessage
  83. Emulates the W3C Browser based WebSocket interface using function members.
  84. ### websocket.addEventListener(method, listener)
  85. Emulates the W3C Browser based WebSocket interface using addEventListener.
  86. ### Event: 'error'
  87. `function (error) { }`
  88. If the client emits an error, this event is emitted (errors from the underlying `net.Socket` are forwarded here).
  89. ### Event: 'close'
  90. `function (code, message) { }`
  91. Is emitted when the connection is closed. `code` is defined in the WebSocket specification.
  92. The `close` event is also emitted when then underlying `net.Socket` closes the connection (`end` or `close`).
  93. ### Event: 'message'
  94. `function (data, flags) { }`
  95. Is emitted when data is received. `flags` is an object with member `binary`.
  96. ### Event: 'ping'
  97. `function (data, flags) { }`
  98. Is emitted when a ping is received. `flags` is an object with member `binary`.
  99. ### Event: 'pong'
  100. `function (data, flags) { }`
  101. Is emitted when a pong is received. `flags` is an object with member `binary`.
  102. ### Event: 'open'
  103. `function () { }`
  104. Emitted when the connection is established.