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
|
# File 'lib/rfid/websocket.rb', line 5
def self.start
@channel = EM::Channel.new
Rfid::Translator.channel = @channel
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
ws.onopen {
socket_id = @channel.subscribe { |msg| ws.send msg }
socket_time = Time.now
ws.send({:event => 'connect', :data => { :sid => socket_id, :state => ws.state }}.to_json)
Rfid::Translator.web_log('socket_opened', socket_id, "Origin: #{ws.request['host']}")
ws.onmessage { |msg|
Rfid::Translator.web_log('client_message', socket_id, "Message", msg)
Rfid::ClientMessage.new(msg).perform
}
ws.onclose {
@channel.unsubscribe(socket_id)
lifetime = Time.now - socket_time
Rfid::Translator.web_log('channel_unsubscribed', socket_id, "Lifetime: #{lifetime}")
}
}
ws.onerror { |error|
Rfid::Translator.web_log('api_error', nil, error.to_s)
}
end
end
|