require "webrick"
include WEBrick
server = HTTPServer.new( :Port => 8053 )
server.mount("/", HTTPServlet::FileHandler, "./docroot")
server.mount_proc("/train/line") do |request, response|
response["Content-Type"] = "text/plain"
response.body = "toot, toot"
end
trap("INT") { server.shutdown }
server.start
http://localhost:8053/train/line
을 방문하면 다음 화면을 볼 수 있을 것이다.
mmlt;bodymmgt;
hello woodinville!
mmlt;/bodymmgt;
...
require "trainspotter"
...
train_spotter = TrainSpotter.new
server.mount_proc("/train/line") do |request, response|
response["Content-Type"] = "text/plain"
json = train_spotter.status_report.
map { |train| "{"track": "" + train.track.to_s + "", "location": " + train.location.to_s + "}" }.
join ","
response.body = "[ #{json} ]"
end
...
class TrainSpotter
def status_report
[ Status.new("south", 20) ]
end
end
class Status
attr_reader :track, :location
def initialize(track, location)
@track = track
@location = location
end
end
http://localhost:8053/train/line
을 방문하면 뭔가 전보다 약간 더 유용한 것을 출력한다.
TRACKS = [:north, :south]
TRAINS_PROGRESS = {:north => 5, :south => 420}
MAX_SPEED = 5
class TrainSpotter
def status_report
report = []
TRAINS_PROGRESS[:north] += rand(MAX_SPEED)
report << Status.new("north", TRAINS_PROGRESS[:north])
TRAINS_PROGRESS[:south] -= rand(MAX_SPEED)
report << Status.new("south", TRAINS_PROGRESS[:south])
end
end
...
http://localhost:8053/train/line
을 방문한 후 반복적으로 새로고침을 하면 데이터가 변하는 것을 볼 수 있다! 이는 Woodinville에서 Redmond로, 남쪽 방향으로 향하는 열차의 진행상태와 Redmond에서 Woodinville로, 북쪽 방향으로 향하는 열차의 진행상태를 함께 보여주고 있다.
mmlt;bodymmgt;
mmlt;div id="status"mmgt;mmlt;/divmmgt;
mmlt;/bodymmgt;
...
mmlt;bodymmgt;
id="redwood"
width="500"
height="120"
style="border: 1px solid black">
mmlt;div id="status"mmgt;mmlt;/divmmgt;
...
...
if ($("redwood").getContext) {
canvas = $("redwood").getContext("2d")
window.setInterval(updateCanvas, 1000 * 2)
updateCanvas()
}
function updateCanvas() {
clearScreen()
drawTracks()
}
function clearScreen() {
canvas.clearRect(0, 0, $("redwood").width, $("redwood").height)
}
function drawTracks() {
...
var trains = {
north: new Train("train-lr.png", 5),
south: new Train("train-rl.png", 60)
}
...
function updateCanvas() {
clearScreen()
drawTracks()
new Ajax.Request("/train/line",
{ onComplete: function(request) {
var jsonData = eval(request.responseText)
if (jsonData == undefined) { return }
jsonData.each(function(data) {
trains[data.track].update(data.location)
})
}
})
}
...
function Train(image, y) {
this.image = new Image()
this.image.src = image
this.y = y
this.update = updateTrain
}
function updateTrain(location) {
canvas.drawImage(this.image, location, this.y)
}
...
...
function updateCanvas() {
new Ajax.Request("/train/line",
{ onComplete: function(request) {
var jsonData = eval(request.responseText)
if (jsonData == undefined) { return }
clearScreen()
jsonData.each(function(data) {
trains[data.track].update(data.location)
})
drawTracks()
drawHotspots()
}
})
}
...
...
server.mount_proc("/train/line") do |request, response|
response["Content-Type"] = "text/plain"
json = train_spotter.status_report.
map { |train| "{"track": "" + train.track.to_s + "", "location": " + train.location.to_s + "}" }.
join ","
response.body = "[ #{json} ]"
end
server.mount_proc("/train/hotspots") do |request, response|
response["Content-Type"] = "text/plain"
json = train_spotter.hot_spots
map { |train| "{"track": "" + train.track.to_s + "", "location": " + train.location.to_s + "}" }.
join ","
response.body = "[ #{json} ]"
end
...
trainspotter.rb
class TrainSpotter
...
def hot_spots
[ Status.new(:north, 125), Status.new(:south, 250), Status.new(:south, 150) ]
end
end
...
def status_list_to_json(list)
json = list.
map { |train| "{"track": "" + train.track.to_s + "", "location": " + train.location.to_s + "}" }.
join ","
"[ #{json} ]"
end
server.mount_proc("/train/line") do |request, response|
response["Content-Type"] = "text/plain"
response.body = status_list_to_json(train_spotter.status_report)
end
server.mount_proc("/train/hotspots") do |request, response|
response["Content-Type"] = "text/plain"
response.body = status_list_to_json(train_spotter.hot_spots)
end
...
http://localhost:8053/train/hotspots
에서 다음과 같은 화면을 볼 수 있다
...
...
이전 글 : Ajax와 자바
다음 글 : WASP을 이용한 PHP 개발
최신 콘텐츠