Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxJava for Grails Async Comet Example
var waitingMsg = false;
function tickTock(){
if(!waitingMsg){
waitingMsg = true;
var xhr = new XMLHttpRequest();
xhr.previous_text = '';
xhr.onreadystatechange = function(){
if(xhr.readyState > 2){
if(xhr.status == 200){
var new_response = xhr.responseText.substring(xhr.previous_text.length);
xhr.previous_text = xhr.responseText;
var elem = document.getElementById('message');
elem.innerHTML = new_response;
}
waitingMsg = false;
}
};
xhr.open('get', '/ticktock', true);
xhr.send();
}
}
tickTock()
class TickTockController {
def index() {
Observable.create({ Subscriber subscriber ->
task {
for(i in (0..20)) {
if(i % 2 == 0) {
subscriber.onNext(
Rx.render("Tick")
)
}
else {
subscriber.onNext(
Rx.render("Tock")
)
}
// the sleep here is to simular a long lived request that takes time etc.
sleep 1000
}
subscriber.onCompleted()
}
} as Observable.OnSubscribe)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.