Class: ActionCable::Connection::ClientSocket
- Inherits:
 - 
      Object
      
        
- Object
 - ActionCable::Connection::ClientSocket
 
 
- Defined in:
 - lib/action_cable/connection/client_socket.rb
 
Overview
– This class is heavily based on faye-websocket-ruby
Copyright © 2010-2015 James Coglan
Constant Summary collapse
- CONNECTING =
 0- OPEN =
 1- CLOSING =
 2- CLOSED =
 3
Instance Attribute Summary collapse
- 
  
    
      #env  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute env.
 - 
  
    
      #url  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute url.
 
Class Method Summary collapse
Instance Method Summary collapse
- #alive? ⇒ Boolean
 - #client_gone ⇒ Object
 - #close(code = nil, reason = nil) ⇒ Object
 - 
  
    
      #initialize(env, event_target, event_loop, protocols)  ⇒ ClientSocket 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of ClientSocket.
 - #parse(data) ⇒ Object
 - #protocol ⇒ Object
 - #rack_response ⇒ Object
 - #start_driver ⇒ Object
 - #transmit(message) ⇒ Object
 - #write(data) ⇒ Object
 
Constructor Details
#initialize(env, event_target, event_loop, protocols) ⇒ ClientSocket
Returns a new instance of ClientSocket.
      34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 34 def initialize(env, event_target, event_loop, protocols) @env = env @event_target = event_target @event_loop = event_loop @url = ClientSocket.determine_url(@env) @driver = @driver_started = nil @close_params = ["", 1006] @ready_state = CONNECTING # The driver calls +env+, +url+, and +write+ @driver = ::WebSocket::Driver.rack(self, protocols: protocols) @driver.on(:open) { |e| open } @driver.on(:message) { |e| (e.data) } @driver.on(:close) { |e| begin_close(e.reason, e.code) } @driver.on(:error) { |e| emit_error(e.) } @stream = ActionCable::Connection::Stream.new(@event_loop, self) end  | 
  
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
      32 33 34  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 32 def env @env end  | 
  
#url ⇒ Object (readonly)
Returns the value of attribute url.
      32 33 34  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 32 def url @url end  | 
  
Class Method Details
.determine_url(env) ⇒ Object
:nodoc:
      12 13 14 15  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 12 def self.determine_url(env) scheme = secure_request?(env) ? "wss:" : "ws:" "#{ scheme }//#{ env['HTTP_HOST'] }#{ env['REQUEST_URI'] }" end  | 
  
.secure_request?(env) ⇒ Boolean
      17 18 19 20 21 22 23 24 25  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 17 def self.secure_request?(env) return true if env["HTTPS"] == "on" return true if env["HTTP_X_FORWARDED_SSL"] == "on" return true if env["HTTP_X_FORWARDED_SCHEME"] == "https" return true if env["HTTP_X_FORWARDED_PROTO"] == "https" return true if env["rack.url_scheme"] == "https" false end  | 
  
Instance Method Details
#alive? ⇒ Boolean
      112 113 114  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 112 def alive? @ready_state == OPEN end  | 
  
#client_gone ⇒ Object
      108 109 110  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 108 def client_gone finalize_close end  | 
  
#close(code = nil, reason = nil) ⇒ Object
      90 91 92 93 94 95 96 97 98 99 100 101 102  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 90 def close(code = nil, reason = nil) code ||= 1000 reason ||= "" unless code == 1000 || (code >= 3000 && code <= 4999) raise ArgumentError, "Failed to execute 'close' on WebSocket: " \ "The code must be either 1000, or between 3000 and 4999. " \ "#{code} is neither." end @ready_state = CLOSING unless @ready_state == CLOSED @driver.close(reason, code) end  | 
  
#parse(data) ⇒ Object
      104 105 106  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 104 def parse(data) @driver.parse(data) end  | 
  
#protocol ⇒ Object
      116 117 118  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 116 def protocol @driver.protocol end  | 
  
#rack_response ⇒ Object
      69 70 71 72  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 69 def rack_response start_driver [ -1, {}, [] ] end  | 
  
#start_driver ⇒ Object
      57 58 59 60 61 62 63 64 65 66 67  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 57 def start_driver return if @driver.nil? || @driver_started @stream.hijack_rack_socket if callback = @env["async.callback"] callback.call([101, {}, @stream]) end @driver_started = true @driver.start end  | 
  
#transmit(message) ⇒ Object
      80 81 82 83 84 85 86 87 88  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 80 def transmit() return false if @ready_state > OPEN case when Numeric then @driver.text(.to_s) when String then @driver.text() when Array then @driver.binary() else false end end  | 
  
#write(data) ⇒ Object
      74 75 76 77 78  | 
    
      # File 'lib/action_cable/connection/client_socket.rb', line 74 def write(data) @stream.write(data) rescue => e emit_error e. end  |