$ bundle install
$ npm install
$ foreman start
Created
August 13, 2012 08:12
-
-
Save uu59/3338127 to your computer and use it in GitHub Desktop.
socket.io and Sinatra
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
vendor/ | |
.bundle/ | |
node_modules/ | |
Gemfile.lock |
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
# -- coding: utf-8 | |
require "json" | |
require "net/http" | |
require "rubygems" | |
require "bundler/setup" | |
Bundler.require | |
use Rack::Parser, :content_types => { | |
"application/json" => proc {|body| JSON.parse(body) } | |
} | |
BRIDGE = JSON.parse(IO.read("bridge.json")) | |
set :port, BRIDGE["rack_port"] | |
get "/" do | |
erb :index | |
end | |
post "/" do | |
Net::HTTP.start('127.0.0.1', BRIDGE["node_port"]) do |http| | |
http.post("/", "triumph=false"); | |
end | |
headers "Location" => "http://localhost:#{BRIDGE["rack_port"]}/" | |
status 302 | |
end | |
post "/report" do | |
if params[:note] == "HUGE SUCCESS" | |
puts "congrats!" | |
end | |
end | |
__END__ | |
@@ layout | |
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<title></title> | |
</head> | |
<body><%= yield %></body></html> | |
@@ index | |
<form action="/" method="post"> | |
post to Sinatra: | |
<input type="submit" value="make it fail" /> | |
</form> | |
websocket emit to Node: | |
<input type="button" id="huge_success" value="HUGE SUCCESS" /> | |
<pre id="received"></pre> | |
<script src="http://localhost:<%= BRIDGE["websocket_port"] %>/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect('http://localhost:<%= BRIDGE["websocket_port"] %>'); | |
var board = document.querySelector('#received'); | |
socket.on('news', function (data) { | |
board.innerHTML += data.message + "\n"; | |
}); | |
document.querySelector('#huge_success').addEventListener('click', function(){ | |
socket.emit('huge success'); | |
}); | |
</script> |
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
{ | |
"rack_port": 4444, | |
"node_port": 4445, | |
"stream_port": 4446 | |
} |
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
source :rubygems | |
gem "rack-parser" | |
gem "sinatra" | |
gem "foreman" |
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
{ | |
"name": "hello-stream", | |
"description": "hello world test app", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"socket.io": "0.9.x" | |
} | |
} |
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
web: ruby ./app.rb | |
node: node ./server.js |
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 fs = require("fs"); | |
var env = JSON.parse(fs.readFileSync("bridge.json").toString()); | |
var io = require("socket.io").listen(env.stream_port, "0.0.0.0"); | |
var http = require("http"); | |
var server = http.createServer(); | |
var msg = "the cake is a lie!"; | |
server.on('request', function(req, res){ | |
if(req.method == "POST" && req.url == "/") { | |
msg = "the cake is a lie!"; | |
} | |
res.writeHead(204); | |
res.end(); | |
}).listen(env.node_port, "0.0.0.0"); | |
io.sockets.on('connection', function(sock) { | |
console.log('new client connected'); | |
sock.on('disconnect', function(){ | |
console.log('disconnect'); | |
}); | |
sock.on('huge success', function(){ | |
msg = "I'm still alive."; | |
var req = http.request({ | |
method: "POST", | |
path: "/report", | |
host: "localhost", | |
port: env.rack_port, | |
headers: { | |
"Content-Type": "application/json" | |
} | |
}) | |
req.write(JSON.stringify({note: "HUGE SUCCESS"})); | |
req.end(); | |
}); | |
}); | |
setInterval(function(){ | |
io.sockets.emit('news', {message: msg}); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nifty! ❤️
Make sure to change
BRIDGE["websocket_port"]
toBRIDGE["stream_port"]
or check out my working fork here.