Class: Brief::Server::Socket
- Inherits:
-
Object
- Object
- Brief::Server::Socket
- Defined in:
- lib/brief/server/socket.rb
Instance Attribute Summary collapse
-
#gateway ⇒ Object
readonly
Returns the value of attribute gateway.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Socket
constructor
A new instance of Socket.
- #log(message) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Socket
Returns a new instance of Socket.
22 23 24 25 26 27 |
# File 'lib/brief/server/socket.rb', line 22 def initialize(={}) = @gateway = gateway @host = .fetch(:host, '0.0.0.0') @port = .fetch(:port, 9099) end |
Instance Attribute Details
#gateway ⇒ Object (readonly)
Returns the value of attribute gateway.
17 18 19 |
# File 'lib/brief/server/socket.rb', line 17 def gateway @gateway end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'lib/brief/server/socket.rb', line 17 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/brief/server/socket.rb', line 17 def end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
17 18 19 |
# File 'lib/brief/server/socket.rb', line 17 def port @port end |
Class Method Details
.start(options = {}, &block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/brief/server/socket.rb', line 2 def self.start(={}, &block) require 'em-websocket' unless defined?(EM::WebSocket) host = .fetch(:host, '0.0.0.0') host = .fetch(:port, '8023') gateway = Brief::Server::Gateway.new() socket = new(gateway: gateway) EM.run { socket.start } end |
Instance Method Details
#log(message) ⇒ Object
29 30 31 32 33 |
# File 'lib/brief/server/socket.rb', line 29 def log() if [:log_to_console] puts() end end |
#start ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/brief/server/socket.rb', line 35 def start log "Starting socket gateway: #{port} host: #{ host }" EM::WebSocket.run(:host => host, :port => port) do |ws| ws.onopen do |handshake| log("== Brief client connected") log(handshake.inspect) end ws.onclose do log("brief Connection closed") end ws. do |data| = OpenStruct.new(JSON.parse(data)) log "== Websocket Command: #{ message }" end end end |