Skip to content

Instantly share code, notes, and snippets.

@theturtle32
Created September 6, 2011 09:41
Show Gist options
  • Select an option

  • Save theturtle32/1197134 to your computer and use it in GitHub Desktop.

Select an option

Save theturtle32/1197134 to your computer and use it in GitHub Desktop.

Revisions

  1. theturtle32 revised this gist Sep 6, 2011. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions ws_streaming_api.md
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,12 @@ Resumes the incoming `data` events after a `pause()`.

    ###pipe(destination, [options])

    This is a Stream.prototype method available on all Streams.

    Connects this read stream to destination WriteStream. Incoming data on this stream gets written to destination. The destination and source streams are kept in sync by pausing and resuming as necessary.

    See the Node.JS documentation on streams for more detail.

    ###setEncoding(encoding)

    Makes the data event emit a string instead of a Buffer. encoding can be `binary`, `utf8`, `ascii`, or `base64`.
  2. theturtle32 revised this gist Sep 6, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ws_streaming_api.md
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,12 @@ Stops emitting `data` events for the incoming message, effectively ignoring the

    ###pause()

    Pauses the incoming `data` events.

    ###resume()

    Resumes the incoming `data` events after a `pause()`.

    ###pipe(destination, [options])

    ###setEncoding(encoding)
  3. theturtle32 created this gist Sep 6, 2011.
    138 changes: 138 additions & 0 deletions ws_streaming_api.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,138 @@
    *The methods and events here are in addition to the ones already defined in the documentation for WebSocket-Node*


    WebSocketConnection
    ===================


    Methods
    -------

    ###newMessage(type)

    `type` can be either `"text"` or `"binary"`. Returns an instance of `WebSocketWritableStream`, a writable stream that represents a single logical WebSocket message.

    **This method replaces both sendBytes(buffer) and sendUTF(string) on `WebSocketConnection`**


    Events
    ------

    ###message(type, stream)

    `type` will be either `"text"` or `"binary"`. `stream` is `WebSocketReadableStream`, a readable stream that represents a single logical WebSocket message.

    **This event replaces the existing 'message' and 'frame' events on `WebSocketConnection`***



    WebSocketReadableStream
    =======================

    Methods
    -------

    ###destroy()

    Stops emitting `data` events for the incoming message, effectively ignoring the rest of the message's content. Any further data received in this message will be silently discarded

    ###pause()

    ###resume()

    ###pipe(destination, [options])

    ###setEncoding(encoding)

    Makes the data event emit a string instead of a Buffer. encoding can be `binary`, `utf8`, `ascii`, or `base64`.


    Events
    ------

    ###data
    `function(data)`

    `data` is a Buffer by default if the message type is `binary` or a string if the message type is `utf8` or after setEncoding() set called with an `encoding` other than `binary`. This event is emitted for each TCP packet or WebSocket frame received for this logical WebSocket "message." When working with a `utf8` message, the stream will need to be aware of utf-8 encoding and be careful to look for a partial utf-8 character at the end of a frame or tcp packet. Some minimal buffering will need to be done on those bytes, prepending them to the following frame or packet in order to continue correctly emitting characters. WebSocket frames are not guaranteed to split cleanly on unicode character boundaries.

    ###end
    `function()`

    Emitted when the WebSocket message is complete and all data has been emitted.

    ###error
    `function(exception)`

    Emitted when there is an error while receiving the WebSocket message. It could be a protocol error or an error on the underlying connection, e.g. if the TCP socket is suddenly closed or a close frame is received before the end of a fragmented message.


    Properties
    ----------

    ###readable

    Always true until an `end` or `error` event has occurred.




    WebSocketWritableStream
    =======================

    Represents a single outgoing logical WebSocket message.

    Methods
    -------

    ###write(string)
    ###write(buffer)

    Writes `string` or `buffer` as a WebSocket logical fragment of the message. If a `String` is passed as the argument, it will be encoded as `utf-8` per the WebSocket specification. No other text encodings are allowed. Returns true if the data has been flushed to the kernel buffer. Returns false to indicate that the kernel buffer is full, and the data will be sent out in the future. The `drain` event will indicate when the kernel buffer is empty again.

    ###end(string)
    ###end(buffer)
    ###end()

    Writes the last (or only) frame of the WebSocket logical message to the socket and closes the stream. The last frame will have its FIN bit set indicating that the message is complete. If you want to send a single frame message, use this method instead of write().

    If no argument is passed to `end()`, a FIN frame will be written to the socket with no payload, completing the WebSocket message.

    After calling `end()`, the stream will not emit any more events.


    ###destroy()
    ###destroySoon()

    Aliases for `end()`


    Events
    ------

    ###drain
    `function()`

    Emitted after a write() method was called that returned false to indicate that it is safe to write again.

    ###error
    `function(exception)`

    Emitted on error with the exception exception. The error is most likely to be an error with the underlying connection, such as attempting to write to a closed socket.

    ###close
    `function()`

    Emitted when the underlying WebSocket connection has been closed.

    ###pipe
    `function()`

    Emitted when the stream is passed to a readable stream's pipe method.


    Properties
    ----------

    ###writable

    A boolean that is true by default, but turns false after an `error` occurred or `end()` / `destroy()` was called.