Skip to content

Instantly share code, notes, and snippets.

@hakunin
Created August 26, 2009 13:27
Show Gist options
  • Save hakunin/175504 to your computer and use it in GitHub Desktop.
Save hakunin/175504 to your computer and use it in GitHub Desktop.
class ChatDemo
def call env
Kernel.load(__FILE__)
method = env["PATH_INFO"].split('/').last || 'index'
send(method, env)
end
def index env
[200, { 'Content-Type' => 'text/html' }, '<html>
<body>
<div id="chat">4et</div>
<script>
function say(what) {
document.getElementById(\'chat\').innerHTML = what;
}
say("helo");
</script>
<iframe src="/comet"></iframe>
'+env.inspect+'
</body></html>']
end
def comet env
[200, { 'Content-Type' => 'text/html' },
CometResponse.new(%w{hello world i am happy to see you})]
end
end
class CometResponse
def initialize lines
@lines = lines
end
def each
yield '<html><body>'
@lines.each { |line|
yield "<script>window.parent.say('#{line}');</script>\n\n"
sleep 0.2
}
yield '</body></html>'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment