Skip to content

Instantly share code, notes, and snippets.

@mechiland
Created June 27, 2013 07:20
Show Gist options
  • Save mechiland/5874568 to your computer and use it in GitHub Desktop.
Save mechiland/5874568 to your computer and use it in GitHub Desktop.
Sinatra listening Jinshuju
require 'sinatra'
require "sinatra/streaming"
set connections: [], loggin: true, bind: '0.0.0.0'
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
settings.connections << out
out.callback { settings.connections.delete(out) }
end
end
get "/" do
erb :index
end
post "/" do
settings.connections.each { |out| out << "data: #{request.body.string}\n\n" }
204
end
__END__
@@ index
<html>
<head>
<title>jinshuju server</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
textarea { width: 308px;
height: 400px;
font-size: 12px;
font-family: monospace;
border: 0px;
background-color: #eee;
margin: 10px;
padding: 10px;;}
</style>
</head>
<body>
<h1>Jinshuju Server</h1>
<div id="data"></div>
<script>
$(function(){
var es = new EventSource("/stream")
es.onmessage = function(e) {
json = JSON.parse(e.data)
json_str = JSON.stringify(json, undefined, 2)
json_str = "<textarea>" + json_str + "</textarea>"
$("#data").append(json_str)
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment