Created
May 30, 2015 07:25
-
-
Save ajaymdesai/4386614c133134a54cf8 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/rufudo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.2/rx.all.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
//see output in the console! | |
console.clear(); | |
var source = Rx.Observable.create(function(observer) { | |
var websocket = | |
new WebSocket("wss://echo.websocket.org"); | |
var id = null; | |
function onOpen(evt) { | |
console.log("CONNECTED"); | |
id = setInterval(function() { | |
doSend("WebSocket rocks"); | |
}, 1000); | |
} | |
function onClose(evt) { | |
console.log("DISCONNECTED"); | |
} | |
function onMessage(evt) { | |
observer.onNext(evt.data); | |
} | |
function onError(evt) { | |
console.log("ERROR: " + evt.data); | |
observer.onError(error); | |
} | |
function doSend(message) { | |
console.log("SENT: " + message); | |
websocket.send(message); | |
} | |
console.log('started'); | |
websocket.onopen = function(evt) { | |
onOpen(evt); | |
}; | |
websocket.onclose = function(evt) { | |
onClose(evt); | |
}; | |
websocket.onmessage = function(evt) { | |
onMessage(evt); | |
}; | |
websocket.onerror = function(evt) { | |
onError(evt); | |
}; | |
setTimeout(function() { | |
observer.onCompleted(); | |
}, 5000); | |
return function() { | |
console.log('disposal called'); | |
clearInterval(id); | |
websocket.close(); | |
}; | |
}); | |
var sub = source.subscribe(function(x) { | |
console.log('next ' + x); | |
}, function(err) { | |
console.error(err); | |
}, function() { | |
console.info('done'); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">//see output in the console! | |
console.clear(); | |
var source = Rx.Observable.create(function(observer) { | |
var websocket = | |
new WebSocket("wss://echo.websocket.org"); | |
var id = null; | |
function onOpen(evt) { | |
console.log("CONNECTED"); | |
id = setInterval(function() { | |
doSend("WebSocket rocks"); | |
}, 1000); | |
} | |
function onClose(evt) { | |
console.log("DISCONNECTED"); | |
} | |
function onMessage(evt) { | |
observer.onNext(evt.data); | |
} | |
function onError(evt) { | |
console.log("ERROR: " + evt.data); | |
observer.onError(error); | |
} | |
function doSend(message) { | |
console.log("SENT: " + message); | |
websocket.send(message); | |
} | |
console.log('started'); | |
websocket.onopen = function(evt) { | |
onOpen(evt); | |
}; | |
websocket.onclose = function(evt) { | |
onClose(evt); | |
}; | |
websocket.onmessage = function(evt) { | |
onMessage(evt); | |
}; | |
websocket.onerror = function(evt) { | |
onError(evt); | |
}; | |
setTimeout(function() { | |
observer.onCompleted(); | |
}, 5000); | |
return function() { | |
console.log('disposal called'); | |
clearInterval(id); | |
websocket.close(); | |
}; | |
}); | |
var sub = source.subscribe(function(x) { | |
console.log('next ' + x); | |
}, function(err) { | |
console.error(err); | |
}, function() { | |
console.info('done'); | |
});</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//see output in the console! | |
console.clear(); | |
var source = Rx.Observable.create(function(observer) { | |
var websocket = | |
new WebSocket("wss://echo.websocket.org"); | |
var id = null; | |
function onOpen(evt) { | |
console.log("CONNECTED"); | |
id = setInterval(function() { | |
doSend("WebSocket rocks"); | |
}, 1000); | |
} | |
function onClose(evt) { | |
console.log("DISCONNECTED"); | |
} | |
function onMessage(evt) { | |
observer.onNext(evt.data); | |
} | |
function onError(evt) { | |
console.log("ERROR: " + evt.data); | |
observer.onError(error); | |
} | |
function doSend(message) { | |
console.log("SENT: " + message); | |
websocket.send(message); | |
} | |
console.log('started'); | |
websocket.onopen = function(evt) { | |
onOpen(evt); | |
}; | |
websocket.onclose = function(evt) { | |
onClose(evt); | |
}; | |
websocket.onmessage = function(evt) { | |
onMessage(evt); | |
}; | |
websocket.onerror = function(evt) { | |
onError(evt); | |
}; | |
setTimeout(function() { | |
observer.onCompleted(); | |
}, 5000); | |
return function() { | |
console.log('disposal called'); | |
clearInterval(id); | |
websocket.close(); | |
}; | |
}); | |
var sub = source.subscribe(function(x) { | |
console.log('next ' + x); | |
}, function(err) { | |
console.error(err); | |
}, function() { | |
console.info('done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment