-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
46 lines (37 loc) · 1.06 KB
/
web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'sinatra'
require 'em-websocket'
@@websocket_server
get '/' do
erb :dash
end
get '/update_frame' do
erb :update_frame
end
post '/update_frame' do
@@websocket_server.send "data = {frame_id:0, size:#{params[:size]}," +
"x:#{params[:x]} ," +
" y:#{params[:y]}," +
" color:'" + params[:color]+ "'," +
" text:'" + params[:text]+ "'}"
erb :update_frame
end
configure do
Thread.new {
EM.run {
EM::WebSocket.run(:host => "0.0.0.0", :port => 9876) do |ws|
@@websocket_server = ws
@@websocket_server.onopen { |handshake|
puts "WebSocket connection open"
# Access properties on the EM::WebSocket::Handshake object, e.g.
# path, query_string, origin, headers
@@websocket_server.send "data = 'Hello Client, you connected !!! to #{handshake.path}'"
}
@@websocket_server.onclose { puts "Connection closed" }
@@websocket_server.onmessage { |msg|
puts "Recieved message: #{msg}"
ws.send "data = 'Pong: #{msg}'"
}
end
}
}
end