Class: WsClient::Client
- Inherits:
- 
      Object
      
        - Object
- WsClient::Client
 
- Includes:
- EventEmitter
- Defined in:
- lib/ws_client.rb
Direct Known Subclasses
Constant Summary collapse
- MS_2 =
- (1/500.0) 
- FRAME_SIZE =
- 2048
Instance Attribute Summary collapse
- 
  
    
      #connect_options  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute connect_options. 
- 
  
    
      #message_queue  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute message_queue. 
- 
  
    
      #url  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute url. 
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect(url = nil, options = {}) ⇒ Object
- 
  
    
      #initialize  ⇒ Client 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Client. 
- #open? ⇒ Boolean
- #reconnect ⇒ Object
- 
  
    
      #send_data_and_wait(data, timeout = MS_2, opt = { :type => :text })  ⇒ Object 
    
    
      (also: #send_data)
    
  
  
  
  
  
  
  
  
  
    Returns all responses that have been accepted. 
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
| 24 25 26 | # File 'lib/ws_client.rb', line 24 def initialize @message_queue = ::Queue.new end | 
Instance Attribute Details
#connect_options ⇒ Object (readonly)
Returns the value of attribute connect_options.
| 19 20 21 | # File 'lib/ws_client.rb', line 19 def @connect_options end | 
#message_queue ⇒ Object (readonly)
Returns the value of attribute message_queue.
| 19 20 21 | # File 'lib/ws_client.rb', line 19 def @message_queue end | 
#url ⇒ Object (readonly)
Returns the value of attribute url.
| 19 20 21 | # File 'lib/ws_client.rb', line 19 def url @url end | 
Instance Method Details
#close ⇒ Object
| 83 84 85 86 87 88 89 90 91 92 93 94 | # File 'lib/ws_client.rb', line 83 def close return if @closed write_data nil, :type => :close if !@pipe_broken emit :close ensure @closed = true @socket.close if @socket @tcp_socket.close if @tcp_socket @socket = nil @tcp_socket = nil end | 
#closed? ⇒ Boolean
| 96 97 98 | # File 'lib/ws_client.rb', line 96 def closed? !open? end | 
#connect(url = nil, options = {}) ⇒ Object
| 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # File 'lib/ws_client.rb', line 28 def connect(url = nil, ={}) return if open? @connect_options = @connect_url = @url = url || @connect_url || @url raise "No URL to connect to" if url.nil? uri = URI.parse url @socket = TCPSocket.new(uri.host, uri.port || (uri.scheme == 'wss' ? 443 : 80)) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if ['https', 'wss'].include?(uri.scheme) ssl_context = [:ssl_context] || begin ctx = OpenSSL::SSL::SSLContext.new ctx.ssl_version = [:ssl_version] || 'SSLv23' ctx.verify_mode = [:verify_mode] || OpenSSL::SSL::VERIFY_NONE #use VERIFY_PEER for verification cert_store = OpenSSL::X509::Store.new cert_store.set_default_paths ctx.cert_store = cert_store ctx end # Keep a handle to the TCPSocket, because the SSLSocket will not clean it up for us. @tcp_socket = @socket @socket = ::OpenSSL::SSL::SSLSocket.new(@tcp_socket, ssl_context) @socket.connect end @pipe_broken = false @closed = false handshake end | 
#open? ⇒ Boolean
| 100 101 102 | # File 'lib/ws_client.rb', line 100 def open? @handshake && @handshake.finished? && !@closed end | 
#reconnect ⇒ Object
| 63 64 65 | # File 'lib/ws_client.rb', line 63 def reconnect connect(nil) end | 
#send_data_and_wait(data, timeout = MS_2, opt = { :type => :text }) ⇒ Object Also known as: send_data
Returns all responses that have been accepted
| 68 69 70 71 72 73 74 75 76 77 78 79 80 | # File 'lib/ws_client.rb', line 68 def send_data_and_wait(data, timeout = MS_2, opt = { :type => :text }) response_data = [] write_data(data, opt) (@socket, timeout) if .length > 0 .length.times do response_data << .pop end end response_data end |