I try to build a billboard for displaying messages in a browser initiated by the server using sinatra. The obviest thing to do in the servercode was like this:
server:
require 'em-websocket'
require 'yajl'
require 'haml'
require 'sinatra/base'
require 'thin'
$channel = EM::Channel.new
EventMachine.run do
class App < Sinatra::Base
get '/' do
haml :index
end
end
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws|
ws.onopen {
sid = $channel.subscribe { |msg| ws.send msg }
ws.onmessage { |msg| $channel.push msg}
# the text I periodically send to the server and want to be visible in the connected webbrowsers
1.upto(5) {|i| sleep 3; ws.send "Hello #{i}"} #had to remove this
}
end
App.run!({:port => 3000})
end
index.haml
!!!
%html{:lang => "en"}
%head
%link{:href => "/css/bootstrap.css", :rel => "stylesheet"}
%body
%div#show
%script{:src => "http://code.jquery.com/jquery-1.7.2.min.js"}
%script{:src => "/js/bootstrap.js"}
%script{:src => "/js/app.js"}
app.js
$(function(){
ws = new WebSocket("ws://localhost:8080");
ws.onmessage = function(evt) {
if (evt.data!='done'){
document.getElementById("show").innerHTML = evt.data;
}
};
});
But the text is shown in the browser all at once after the last sleep, so only the last line is visible. I can't seem to send stuff from the serverside once the connection is made, only as reaction to an incomming message.
So I tried it with a Ruby client like below which worked but I have to make a new connection every time I send some text, putting the loop in the .callback or .stream method doesn't work.
require 'em-websocket-client'
def send text
EM.run do
conn = EventMachine::WebSocketClient.connect("ws://localhost:8080")
conn.callback do
conn.send_msg text
conn.send_msg "done"
end
conn.stream do |msg|
puts "<#{msg}>"
if msg.data == "done"
conn.close_connection
end
end
conn.disconnect do
EM::stop_event_loop
end
end
end
1.upto(5) do |i|
sleep 3
send "Hello #{i}"
end
I'm sure there has to better way to achive this, which way please ?