Class: AnyCable::RPCHandler

Inherits:
AnyCable::RPC::Service show all
Includes:
Handler::CaptureExceptions
Defined in:
lib/anycable/rpc_handler.rb

Overview

RPC service handler

Constant Summary

Constants included from Handler::CaptureExceptions

Handler::CaptureExceptions::RESPONSE_CLASS

Instance Method Summary collapse

Methods included from Handler::CaptureExceptions

#capture_exceptions

Instance Method Details

#command(message, _unused_call) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/anycable/rpc_handler.rb', line 55

def command(message, _unused_call)
  logger.debug("RPC Command: #{message.inspect}")

  socket = build_socket

  connection = factory.call(
    socket,
    identifiers: message.connection_identifiers
  )

  result = connection.handle_channel_command(
    message.identifier,
    message.command,
    message.data
  )

  AnyCable::CommandResponse.new(
    status: result ? AnyCable::Status::SUCCESS : AnyCable::Status::FAILURE,
    disconnect: socket.closed?,
    stop_streams: socket.stop_streams?,
    streams: socket.streams,
    transmissions: socket.transmissions
  )
end

#connect(request, _unused_call) ⇒ Object

Handle connection request from WebSocket server



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 17

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

  socket = build_socket(env: rack_env(request))

  connection = factory.call(socket)

  connection.handle_open

  if socket.closed?
    AnyCable::ConnectionResponse.new(status: AnyCable::Status::FAILURE)
  else
    AnyCable::ConnectionResponse.new(
      status: AnyCable::Status::SUCCESS,
      identifiers: connection.identifiers_json,
      transmissions: socket.transmissions
    )
  end
end

#disconnect(request, _unused_call) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/anycable/rpc_handler.rb', line 37

def disconnect(request, _unused_call)
  logger.debug("RPC Disconnect: #{request.inspect}")

  socket = build_socket(env: rack_env(request))

  connection = factory.call(
    socket,
    identifiers: request.identifiers,
    subscriptions: request.subscriptions
  )

  if connection.handle_close
    AnyCable::DisconnectResponse.new(status: AnyCable::Status::SUCCESS)
  else
    AnyCable::DisconnectResponse.new(status: AnyCable::Status::FAILURE)
  end
end