Class: AnyCable::Rack::Connection

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/anycable/rack/connection.rb

Overview

:nodoc:

Constant Summary

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, hub:, coder:, rpc_client:, headers:) ⇒ Connection

Returns a new instance of Connection.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/anycable/rack/connection.rb', line 23

def initialize(socket, hub:, coder:, rpc_client:, headers:)
  @socket = socket
  @coder = coder
  @headers = headers
  @hub = hub
  @sid = SecureRandom.hex(6)

  @rpc_client = rpc_client

  @_identifiers = "{}"
  @_subscriptions = Set.new
  @_istate = {}
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



16
17
18
# File 'lib/anycable/rack/connection.rb', line 16

def coder
  @coder
end

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/anycable/rack/connection.rb', line 16

def headers
  @headers
end

#hubObject (readonly)

Returns the value of attribute hub.



16
17
18
# File 'lib/anycable/rack/connection.rb', line 16

def hub
  @hub
end

#rpc_clientObject (readonly)

Returns the value of attribute rpc_client.



16
17
18
# File 'lib/anycable/rack/connection.rb', line 16

def rpc_client
  @rpc_client
end

#sidObject (readonly)

Returns the value of attribute sid.



16
17
18
# File 'lib/anycable/rack/connection.rb', line 16

def sid
  @sid
end

#socketObject (readonly)

Returns the value of attribute socket.



16
17
18
# File 'lib/anycable/rack/connection.rb', line 16

def socket
  @socket
end

Instance Method Details

#handle_closeObject



42
43
44
45
46
# File 'lib/anycable/rack/connection.rb', line 42

def handle_close
  response = rpc_disconnect
  process_close(response)
  reset_connection
end

#handle_command(websocket_message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/anycable/rack/connection.rb', line 48

def handle_command(websocket_message)
  decoded = decode(websocket_message)
  command = decoded.delete("command")

  channel_identifier = decoded["identifier"]

  log(:debug) { "Command: #{decoded}" }

  case command
  when "subscribe" then subscribe(channel_identifier)
  when "unsubscribe" then unsubscribe(channel_identifier)
  when "message" then send_message(channel_identifier, decoded["data"])
  else
    log(:error, "Command not found #{command}")
  end
rescue Exception => e # rubocop:disable Lint/RescueException
  log(:error, "Failed to execute command #{command}: #{e.message}")
end

#handle_openObject



37
38
39
40
# File 'lib/anycable/rack/connection.rb', line 37

def handle_open
  response = rpc_connect
  process_open(response)
end