Class: AnyCable::RackServer::Connection

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

Constant Summary

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, hub, coder, host, header_names, server_id) ⇒ Connection

Returns a new instance of Connection.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/anycable/rack-server/connection.rb', line 21

def initialize(socket, hub, coder, host, header_names, server_id)
  @socket       = socket
  @coder        = coder
  @hub          = hub
  @header_names = header_names
  @server_id    = server_id

  @rpc_client = RPC::Client.new(host)

  @_identifiers   = '{}'
  @_subscriptions = Set.new
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



14
15
16
# File 'lib/anycable/rack-server/connection.rb', line 14

def coder
  @coder
end

#header_namesObject (readonly)

Returns the value of attribute header_names.



14
15
16
# File 'lib/anycable/rack-server/connection.rb', line 14

def header_names
  @header_names
end

#hubObject (readonly)

Returns the value of attribute hub.



14
15
16
# File 'lib/anycable/rack-server/connection.rb', line 14

def hub
  @hub
end

#rpc_clientObject (readonly)

Returns the value of attribute rpc_client.



14
15
16
# File 'lib/anycable/rack-server/connection.rb', line 14

def rpc_client
  @rpc_client
end

#server_idObject (readonly)

Returns the value of attribute server_id.



14
15
16
# File 'lib/anycable/rack-server/connection.rb', line 14

def server_id
  @server_id
end

#socketObject (readonly)

Returns the value of attribute socket.



14
15
16
# File 'lib/anycable/rack-server/connection.rb', line 14

def socket
  @socket
end

Instance Method Details

#handle_closeObject



39
40
41
42
43
# File 'lib/anycable/rack-server/connection.rb', line 39

def handle_close
  response = rpc_disconnect
  process_close(response)
  reset_connection
end

#handle_command(websocket_message) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/anycable/rack-server/connection.rb', line 45

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

  channel_identifier = decoded['identifier']

  case command
  when 'subscribe'   then subscribe(channel_identifier)
  when 'unsubscribe' then unsubscribe(channel_identifier)
  when 'message'     then send_message(channel_identifier, decoded['data'])
  else
    raise Errors::UnknownCommand, "Command not found #{command}"
  end
end

#handle_openObject



34
35
36
37
# File 'lib/anycable/rack-server/connection.rb', line 34

def handle_open
  response = rpc_connect
  process_open(response)
end