Created
June 15, 2011 22:06
-
-
Save coop/1028260 to your computer and use it in GitHub Desktop.
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
require 'goliath' | |
class Hello < Goliath::API | |
def on_close(env) | |
env.logger.info "Connection closed." | |
end | |
def response(env) | |
i = 0 | |
pt = EM.add_periodic_timer(1) do | |
env.stream_send("#{i}\n") | |
i += 1 | |
end | |
# EM.add_timer(10) do | |
# pt.cancel | |
# | |
# env.stream_send("!! BOOM !!\n") | |
# env.stream_close | |
# end | |
[200, {}, Goliath::Response::STREAMING] | |
end | |
end | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Web Sockets Example</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> | |
<script> | |
(function($) { | |
var socket = new WebSocket("ws://localhost:9000/") | |
socket.onopen = function(event) { | |
console.log("open") | |
} | |
socket.onmessage = function(event) { | |
console.log(msg) | |
} | |
socket.onclose = function(event) { | |
console.log(event) | |
} | |
socket.onerror = function(event) { | |
console.log(event) | |
} | |
})(jQuery); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment