Skip to content

Latest commit

 

History

History
72 lines (57 loc) · 2.24 KB

fromwebsocket.md

File metadata and controls

72 lines (57 loc) · 2.24 KB

Rx.DOM.fromWebSocket(url, protocol, [openObserver], [closeObserver])

Creates a WebSocket Subject with a given URL, protocol and an optional observer for the open event and for close.

Arguments

  1. url (String): The URL of the WebSocket.
  2. protocol (String): The protocol of the WebSocket.
  3. openObserver (Rx.Observer): An optional Observer to capture the open event.
  4. closingObserver (Rx.Observer): An optional Observer capture the the moment before the underlying socket is closed.

Returns

(Subject): A Subject which wraps a WebSocket.

Example

// an observer for when the socket is open
var openObserver = Rx.Observer.create(function(e) {
  console.info('socket open');

  // Now it is safe to send a message
  socket.onNext('test');
});

// an observer for when the socket is about to close
var closingObserver = Rx.Observer.create(function() {
  console.log('socket is about to close');
});

// create a web socket subject
socket = Rx.DOM.fromWebSocket(
  'ws://echo.websockets.org',
  null, // no protocol
  openObserver,
  closingObserver);

// subscribing creates the underlying socket and will emit a stream of incoming
// message events
socket.subscribe(
  function(e) {
    console.log('message: %s', e.data);
  },
  function(e) {
    // errors and "unclean" closes land here
    console.error('error: %s', e);
  },
  function() {
    // the socket has been closed
    console.info('socket closed');
  }
);

Location

File:

Dist:

Prerequisites:

NPM Packages:

NuGet Packages:

Unit Tests: