Skip to content

Instantly share code, notes, and snippets.

@coop
Created June 15, 2011 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coop/1028260 to your computer and use it in GitHub Desktop.
Save coop/1028260 to your computer and use it in GitHub Desktop.
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