Skip to content
This repository
Browse code

Merge pull request #49 from nicokaiser/master

Add some docs for the classes
  • Loading branch information...
commit b29aab011faca7ee37fc01f846a1acc28b18c30d 2 parents 17c8556 + bb3a255
Einar Otto Stangvik authored

Showing 2 changed files with 157 additions and 14 deletions. Show diff stats Hide diff stats

  1. +1 14 README.md
  2. +156 0 doc/ws.md
15 README.md
Source Rendered
@@ -105,21 +105,8 @@ Otherwise, see the test cases.
105 105
106 106 ## API Docs ##
107 107
108   -_Note: This api documentation is currently incomplete. For a better understanding of the api, see the test set._
  108 +See the doc/ directory for Node.js-like docs for the ws classes.
109 109
110   -### WebSocket
111   -
112   -`supports.binary`
113   -
114   -> Returns true or false based on whether the underlying protocol (hixie, hybi, etc) has binary support.
115   -Example:
116   -
117   -```js
118   -var wss = new WebSocketServer({port: 8080});
119   -wss.on('connection', function(client) {
120   - if (client.supports.binary) ...
121   -});
122   -```
123 110
124 111 ## Todos ##
125 112
156 doc/ws.md
Source Rendered
... ... @@ -0,0 +1,156 @@
  1 +# ws
  2 +
  3 +## Class: ws.Server
  4 +
  5 +This class is a WebSocket server. It is an `EventEmitter`.
  6 +
  7 +### new ws.Server([options], [callback])
  8 +
  9 +* `options` Object
  10 + * `host` String
  11 + * `port` Number
  12 + * `server` http.Server
  13 + * `verifyClient` Function
  14 + * `path` String
  15 + * `noServer` Boolean
  16 + * `disableHixie` Boolean
  17 + * `clientTracking` Boolean
  18 +* `callback` Function
  19 +
  20 +Construct a new server object.
  21 +
  22 +Either `port` or `server` must be provided, otherwise you might enable `noServer` if you want to pass the requests directly.
  23 +
  24 +### server.close([code], [data])
  25 +
  26 +Close the server and terminate all clients
  27 +
  28 +### server.handleUpgrade(request, socket, upgradeHead, callback)
  29 +
  30 +Handles a HTTP Upgrade request. `request` is an instance of `http.ServerRequest`, `socket` is an instance of `net.Socket`.
  31 +
  32 +When the Upgrade was successfully, the `callback` will be called with a `ws.WebSocket` object as parameter.
  33 +
  34 +### Event: 'error'
  35 +
  36 +`function (error) { }`
  37 +
  38 +If the underlying server emits an error, it will be forwarded here.
  39 +
  40 +### Event: 'connection'
  41 +
  42 +`function (socket) { }`
  43 +
  44 +When a new WebSocket connection is established. `socket` is an object of type `ws.WebSocket`.
  45 +
  46 +
  47 +## Class: ws.WebSocket
  48 +
  49 +This class represents a WebSocket connection. It is an `EventEmitter`.
  50 +
  51 +### new ws.WebSocket(address, [options])
  52 +
  53 +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`).
  54 +
  55 +### websocket.bytesReceived
  56 +
  57 +Received bytes count.
  58 +
  59 +### websocket.readyState
  60 +
  61 +Possible states are `WebSocket.CONNECTING`, `WebSocket.OPEN`, `WebSocket.CLOSING`, `WebSocket.CLOSED`.
  62 +
  63 +### websocket.protocolVersion
  64 +
  65 +The WebSocket protocol version used for this connection, `8`, `13` or `hixie-76` (the latter only for server clients).
  66 +
  67 +### websocket.url
  68 +
  69 +The URL of the WebSocket server (only for clients)
  70 +
  71 +### websocket.supports
  72 +
  73 +Describes the feature of the used protocol version. E.g. `supports.binary` is a boolean that describes if the connection supports binary messages.
  74 +
  75 +### websocket.close([code], [data])
  76 +
  77 +Gracefully closes the connection, after sending a description message
  78 +
  79 +### websocket.pause()
  80 +
  81 +Pause the client stream
  82 +
  83 +### websocket.ping([data], [options], [dontFailWhenClosed])
  84 +
  85 +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.
  86 +
  87 +### websocket.pong([data], [options], [dontFailWhenClosed])
  88 +
  89 +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.
  90 +
  91 +
  92 +### websocket.resume()
  93 +
  94 +Resume the client stream
  95 +
  96 +### websocket.send(data, [options], [callback])
  97 +
  98 +Sends `data` through the connection. `options` can be an object with members `mask` and `binary`. The optional `callback` is executed after the send completes.
  99 +
  100 +### websocket.stream([options], callback)
  101 +
  102 +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)`.
  103 +
  104 +### websocket.terminate()
  105 +
  106 +Immediately shuts down the connection
  107 +
  108 +### websocket.onopen
  109 +### websocket.onerror
  110 +### websocket.onclose
  111 +### websocket.onmessage
  112 +
  113 +Emulates the W3C Browser based WebSocket interface using function members.
  114 +
  115 +### websocket.addEventListener(method, listener)
  116 +
  117 +Emulates the W3C Browser based WebSocket interface using addEventListener.
  118 +
  119 +### Event: 'error'
  120 +
  121 +`function (error) { }`
  122 +
  123 +If the client emits an error, this event is emitted (errors from the underlying `net.Socket` are forwarded here).
  124 +
  125 +### Event: 'close'
  126 +
  127 +`function (code, message) { }`
  128 +
  129 +Is emitted when the connection is closed. `code` is defined in the WebSocket specification.
  130 +
  131 +The `close` event is also emitted when then underlying `net.Socket` closes the connection (`end` or `close`).
  132 +
  133 +### Event: 'message'
  134 +
  135 +`function (data, flags) { }`
  136 +
  137 +Is emitted when data is received. `flags` is an object with member `binary`.
  138 +
  139 +### Event: 'ping'
  140 +
  141 +`function (data, flags) { }`
  142 +
  143 +Is emitted when a ping is received. `flags` is an object with member `binary`.
  144 +
  145 +### Event: 'pong'
  146 +
  147 +`function (data, flags) { }`
  148 +
  149 +Is emitted when a pong is received. `flags` is an object with member `binary`.
  150 +
  151 +### Event: 'open'
  152 +
  153 +`function () { }`
  154 +
  155 +Emitted when the connection is established.
  156 +

0 comments on commit b29aab0

Please sign in to comment.
Something went wrong with that request. Please try again.