RxJava for Grails Async Comet Example
This file contains 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
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() |
This file contains 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
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