Creates a WebSocket Subject with a given URL, protocol and an optional observer for the open event and for close.
url
(String): The URL of the WebSocket.protocol
(String): The protocol of the WebSocket.openObserver
(Rx.Observer
): An optional Observer to capture the open event.closingObserver
(Rx.Observer
): An optional Observer capture the the moment before the underlying socket is closed.
(Subject
): A Subject which wraps a WebSocket.
// 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');
}
);
File:
Dist:
Prerequisites:
NPM Packages:
NuGet Packages:
Unit Tests: