-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
48 lines (36 loc) · 848 Bytes
/
app.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
47
48
require 'thin'
require 'em-websocket'
require 'sinatra/base'
require 'serialport'
require 'active_support/all'
EM.run do
class App < Sinatra::Base
get '/' do
erb :index
end
end
@clients = []
sp = SerialPort.new('/dev/cu.usbmodemfd121', 9600, 8, 1, SerialPort::NONE)
EM::WebSocket.start(:host => '0.0.0.0', :port => '3001') do |ws|
ws.onopen do |handshake|
@clients << ws
ws.send "Connected to #{handshake.path}."
end
ws.onclose do
ws.send "Closed."
@clients.delete ws
end
ws.onmessage do |msg|
puts "Received Message: #{msg}"
@clients.each do |socket|
socket.send message_from(sp).to_json
end
end
def message_from(sp)
message = sp.gets
message.chop!
{ "event" => message }
end
end
App.run! :port => 3000
end