123456789_123456789_123456789_123456789_123456789_

Class: ActionCable::Connection::ClientSocket

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: actioncable/lib/action_cable/connection/client_socket.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(env, event_target, event_loop, protocols) ⇒ ClientSocket

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 36

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| receive_message(e.data) }
  @driver.on(:close)   { |e| begin_close(e.reason, e.code) }
  @driver.on(:error)   { |e| emit_error(e.message) }

  @stream = ActionCable::Connection::Stream.new(@event_loop, self)
end

Class Method Details

.determine_url(env)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 14

def self.determine_url(env)
  scheme = secure_request?(env) ? "wss:" : "ws:"
  "#{ scheme }//#{ env['HTTP_HOST'] }#{ env['REQUEST_URI'] }"
end

.secure_request?(env) ⇒ Boolean

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 19

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 Attribute Details

#alive?Boolean (readonly)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 114

def alive?
  @ready_state == OPEN
end

#env (readonly)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 34

attr_reader :env, :url

#url (readonly)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 34

attr_reader :env, :url

Instance Method Details

#begin_close(reason, code) (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 142

def begin_close(reason, code)
  return if @ready_state == CLOSED
  @ready_state = CLOSING
  @close_params = [reason, code]

  @stream.shutdown if @stream
  finalize_close
end

#client_gone

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 110

def client_gone
  finalize_close
end

#close(code = nil, reason = nil)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 92

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

#emit_error(message) (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 136

def emit_error(message)
  return if @ready_state >= CLOSING

  @event_target.on_error(message)
end

#finalize_close (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 151

def finalize_close
  return if @ready_state == CLOSED
  @ready_state = CLOSED

  @event_target.on_close(*@close_params)
end

#open (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 123

def open
  return unless @ready_state == CONNECTING
  @ready_state = OPEN

  @event_target.on_open
end

#parse(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 106

def parse(data)
  @driver.parse(data)
end

#protocol

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 118

def protocol
  @driver.protocol
end

#rack_response

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 71

def rack_response
  start_driver
  [ -1, {}, [] ]
end

#receive_message(data) (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 130

def receive_message(data)
  return unless @ready_state == OPEN

  @event_target.on_message(data)
end

#start_driver

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 59

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)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 82

def transmit(message)
  return false if @ready_state > OPEN
  case message
  when Numeric then @driver.text(message.to_s)
  when String  then @driver.text(message)
  when Array   then @driver.binary(message)
  else false
  end
end

#write(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/client_socket.rb', line 76

def write(data)
  @stream.write(data)
rescue => e
  emit_error e.message
end