Class: Patch::IO::Websocket::Socket
- Inherits:
-
Object
- Object
- Patch::IO::Websocket::Socket
- Defined in:
- lib/patch/io/websocket/socket.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#active? ⇒ Boolean
Is the socket active?.
- #disable ⇒ Boolean
-
#initialize ⇒ Socket
constructor
A new instance of Socket.
- #on_message(&callback) ⇒ Boolean
- #puts(data) ⇒ Object
-
#start(config, &block) ⇒ Boolean
Start the websocket.
Constructor Details
#initialize ⇒ Socket
Returns a new instance of Socket.
15 16 17 |
# File 'lib/patch/io/websocket/socket.rb', line 15 def initialize @onmessage = [] end |
Class Method Details
.start(config) ⇒ Object
9 10 11 12 13 |
# File 'lib/patch/io/websocket/socket.rb', line 9 def self.start(config) socket = new socket.start(config) socket end |
Instance Method Details
#active? ⇒ Boolean
Is the socket active?
58 59 60 |
# File 'lib/patch/io/websocket/socket.rb', line 58 def active? !@socket.nil? end |
#disable ⇒ Boolean
24 25 26 27 28 |
# File 'lib/patch/io/websocket/socket.rb', line 24 def disable @socket. = nil @onmessage.clear true end |
#on_message(&callback) ⇒ Boolean
32 33 34 35 36 37 38 39 |
# File 'lib/patch/io/websocket/socket.rb', line 32 def (&callback) if @socket.nil? @onmessage << callback else @socket. { |data| yield(data) } end true end |
#puts(data) ⇒ Object
19 20 21 |
# File 'lib/patch/io/websocket/socket.rb', line 19 def puts(data) @socket.send(data) end |
#start(config, &block) ⇒ Boolean
Start the websocket
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/patch/io/websocket/socket.rb', line 44 def start(config, &block) EM::WebSocket.run(config) do |websocket| ::Thread.current.abort_on_exception = true begin enable(websocket) rescue Exception => exception ::Thread.main.raise(exception) end end true end |