Class: Anycable::RPCHandler

Inherits:
Anycable::RPC::Service show all
Defined in:
lib/anycable/rpc_handler.rb

Overview

RPC service handler

Instance Method Summary collapse

Instance Method Details

#connect(request, _unused_call) ⇒ Object

Handle connection request from WebSocket server



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/anycable/rpc_handler.rb', line 14

def connect(request, _unused_call)
  logger.debug("RPC Connect: #{request}")

  connection = ApplicationCable::Connection.new(
    env:
      path_env(request.path).merge(
        'HTTP_COOKIE' => request.headers['Cookie']
      )
  )

  connection.handle_open

  if connection.closed?
    Anycable::ConnectionResponse.new(status: Anycable::Status::ERROR)
  else
    Anycable::ConnectionResponse.new(
      status: Anycable::Status::SUCCESS,
      identifiers: connection.identifiers_hash.to_json,
      transmissions: connection.transmissions
    )
  end
end

#disconnect(request, _unused_call) ⇒ Object



37
38
39
40
41
# File 'lib/anycable/rpc_handler.rb', line 37

def disconnect(request, _unused_call)
  logger.debug("RPC Disonnect: #{request}")
  # TODO: implement disconnect logic
  Anycable::DisconnectResponse.new(status: Anycable::Status::SUCCESS)
end

#perform(message, _unused_call) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/anycable/rpc_handler.rb', line 84

def perform(message, _unused_call)
  logger.debug("RPC Perform: #{message}")
  connection = ApplicationCable::Connection.new(
    identifiers_json: message.connection_identifiers
  )

  channel = channel_for(connection, message)

  if channel.present?
    channel.perform_action(ActiveSupport::JSON.decode(message.data))
    Anycable::CommandResponse.new(
      status: Anycable::Status::SUCCESS,
      disconnect: connection.closed?,
      stop_streams: channel.stop_streams?,
      streams: channel.streams,
      transmissions: connection.transmissions
    )
  else
    Anycable::CommandResponse.new(
      status: Anycable::Status::ERROR
    )
  end
end

#subscribe(message, _unused_call) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/anycable/rpc_handler.rb', line 43

def subscribe(message, _unused_call)
  logger.debug("RPC Subscribe: #{message}")
  connection = ApplicationCable::Connection.new(
    identifiers_json: message.connection_identifiers
  )

  channel = channel_for(connection, message)

  if channel.present?
    channel.do_subscribe
    if channel.subscription_rejected?
      Anycable::CommandResponse.new(
        status: Anycable::Status::ERROR,
        disconnect: connection.closed?,
        transmissions: connection.transmissions
      )
    else
      Anycable::CommandResponse.new(
        status: Anycable::Status::SUCCESS,
        disconnect: connection.closed?,
        stop_streams: channel.stop_streams?,
        streams: channel.streams,
        transmissions: connection.transmissions
      )
    end
  else
    Anycable::CommandResponse.new(
      status: Anycable::Status::ERROR
    )
  end
end

#unsubscribe(message, _unused_call) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/anycable/rpc_handler.rb', line 75

def unsubscribe(message, _unused_call)
  logger.debug("RPC Unsubscribe: #{message}")
  Anycable::CommandResponse.new(
    status: Anycable::Status::SUCCESS,
    disconnect: false,
    stop_streams: true
  )
end