class Patch::IO::Websocket::Socket
Public Class Methods
new()
click to toggle source
# File lib/patch/io/websocket/socket.rb, line 15 def initialize @onmessage = [] end
start(config)
click to toggle source
# File lib/patch/io/websocket/socket.rb, line 9 def self.start(config) socket = new socket.start(config) socket end
Public Instance Methods
active?()
click to toggle source
Is the socket active? @return [Boolean]
# File lib/patch/io/websocket/socket.rb, line 58 def active? !@socket.nil? end
disable()
click to toggle source
@return [Boolean]
# File lib/patch/io/websocket/socket.rb, line 24 def disable @socket.onmessage = nil @onmessage.clear true end
on_message() { |data| ... }
click to toggle source
@param [Proc] callback callback to fire when events are received @return [Boolean]
# File lib/patch/io/websocket/socket.rb, line 32 def on_message(&callback) if @socket.nil? @onmessage << callback else @socket.onmessage { |data| yield(data) } end true end
puts(data)
click to toggle source
# File lib/patch/io/websocket/socket.rb, line 19 def puts(data) @socket.send(data) end
start(config, &block)
click to toggle source
Start the websocket @param [Hash] config @return [Boolean]
# 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
Private Instance Methods
configure()
click to toggle source
Configure the server actions @return [Boolean]
# File lib/patch/io/websocket/socket.rb, line 83 def configure @socket.onopen do |handshake| puts "Connection open" end @socket.onclose do puts "Connection closed" end configure_message_callbacks unless @onmessage.empty? true end
configure_message_callbacks()
click to toggle source
If callbacks were added before the socket was active, assign them to the socket event handler
# File lib/patch/io/websocket/socket.rb, line 65 def configure_message_callbacks @onmessage.each do |callback| on_message(&callback) end @onmessage.clear end
enable(websocket)
click to toggle source
Enable this node after initializing an EM::Websocket @param [EM::Websocket] websocket @return [Boolean]
# File lib/patch/io/websocket/socket.rb, line 75 def enable(websocket) @socket = websocket configure true end