class XRBP::WebSocket::Connection
Primary websocket interface, use Connection
to perform websocket requests.
@example retrieve data via a websocket
connection = WebSocket::Connection.new "wss://s1.ripple.com:443" puts connection.send_data('{"command" : "server_info"}')
Attributes
parent[RW]
url[R]
Public Class Methods
new(url) { |self| ... }
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 23 def initialize(url) @url = url @force_quit = false yield self if block_given? end
Public Instance Methods
add_work(&bl)
click to toggle source
Add work to the internal client thread pool
@private
# File lib/xrbp/websocket/connection.rb, line 48 def add_work(&bl) client.add_work &bl end
async_close!()
click to toggle source
Close in a non-blocking way, and immediately return.
# File lib/xrbp/websocket/connection.rb, line 80 def async_close! client.async_close if open? end
client()
click to toggle source
@private
# File lib/xrbp/websocket/connection.rb, line 150 def client @client ||= begin client = Client.new(@url) conn = self client.on :connecting do conn.emit :connecting conn.parent.emit :connecting, conn if conn.parent end client.on :open do conn.emit :open conn.parent.emit :open, conn if conn.parent conn.state_mutex.synchronize { conn.open_cv.signal } conn.plugins.each { |plg| plg.opened if plg.respond_to?(:opened) } end client.on :close do conn.emit :close conn.parent.emit :close, conn if conn.parent conn.state_mutex.synchronize { conn.close_cv.signal } conn.plugins.each { |plg| plg.closed if plg.respond_to?(:closed) } end client.on :completed do |err| conn.emit :completed conn.parent.emit :completed, conn if conn.parent conn.state_mutex.synchronize { conn.completed_cv.signal } conn.plugins.each { |plg| plg.completed if plg.respond_to?(:completed) } end client.on :error do |err| conn.emit :error, err conn.parent.emit :error, conn, err if conn.parent conn.plugins.each { |plg| plg.error err if plg.respond_to?(:error) } end client.on :message do |msg| conn.emit :message, msg conn.parent.emit :message, conn, msg if conn.parent conn.plugins.each { |plg| plg.message msg if plg.respond_to?(:message) } end client end end
close!()
click to toggle source
Close the connection, blocking until completed
# File lib/xrbp/websocket/connection.rb, line 75 def close! client.close if open? end
close_cv()
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 139 def close_cv @close_cv ||= ConditionVariable.new end
closed?()
click to toggle source
Indicates the connection is closed (may not be completed)
# File lib/xrbp/websocket/connection.rb, line 64 def closed? !open? end
completed?()
click to toggle source
Indicates if connection is completely closed and cleaned up
# File lib/xrbp/websocket/connection.rb, line 70 def completed? client.completed? end
completed_cv()
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 143 def completed_cv @completed_cv ||= ConditionVariable.new end
connect()
click to toggle source
Initiate new client connection
# File lib/xrbp/websocket/connection.rb, line 33 def connect client.connect end
force_quit!()
click to toggle source
Immediately terminate the connection and all related operations
# File lib/xrbp/websocket/connection.rb, line 96 def force_quit! @force_quit = true wake_all # TODO immediate terminate socket connection end
force_quit?()
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 91 def force_quit? @force_quit end
initialized?()
click to toggle source
Indicates the connection is initialized
# File lib/xrbp/websocket/connection.rb, line 53 def initialized? !!@client end
next_connection(prev)
click to toggle source
Return next connection of parent if applicable
@private
# File lib/xrbp/websocket/connection.rb, line 40 def next_connection(prev) return nil unless !!parent parent.next_connection(prev) end
open?()
click to toggle source
Indicates the connection is open
# File lib/xrbp/websocket/connection.rb, line 58 def open? initialized? && client.open? end
open_cv()
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 135 def open_cv @open_cv ||= ConditionVariable.new end
plugin_namespace()
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 16 def plugin_namespace WebSocket end
send_data(data)
click to toggle source
Send raw data via this connection
# File lib/xrbp/websocket/connection.rb, line 85 def send_data(data) client.send_data(data) end
state_mutex()
click to toggle source
# File lib/xrbp/websocket/connection.rb, line 131 def state_mutex @state_mutex ||= Mutex.new end
wait_for_close()
click to toggle source
Block until connection is closed
# File lib/xrbp/websocket/connection.rb, line 114 def wait_for_close return unless initialized? state_mutex.synchronize { close_cv.wait(state_mutex, 0.1) } while !force_quit? && open? end
wait_for_completed()
click to toggle source
Block until connection is completed
# File lib/xrbp/websocket/connection.rb, line 123 def wait_for_completed return unless initialized? state_mutex.synchronize { completed_cv.wait(state_mutex, 0.1) } while !force_quit? && !completed? end
wait_for_open()
click to toggle source
Block until connection is open
# File lib/xrbp/websocket/connection.rb, line 105 def wait_for_open return unless initialized? state_mutex.synchronize { open_cv.wait(state_mutex, 0.1) } until force_quit? || open? end