Class: Meshchat::Network::Remote::Relay
- Inherits:
-
Object
- Object
- Meshchat::Network::Remote::Relay
- Defined in:
- lib/meshchat/network/remote/relay.rb
Constant Summary collapse
- CHANNEL =
This channel is determine by the server, see github.com/NullVoxPopuli/mesh-relay/blob/master/app/channels/mesh_relay_channel.rb
'MeshRelayChannel'
Instance Attribute Summary collapse
-
#_client ⇒ Object
readonly
Returns the value of attribute _client.
-
#_connected ⇒ Object
Returns the value of attribute _connected.
-
#_request_processor ⇒ Object
readonly
Returns the value of attribute _request_processor.
-
#_url ⇒ Object
readonly
Returns the value of attribute _url.
Instance Method Summary collapse
- #chat_message_received(message) ⇒ Object
- #connected? ⇒ Boolean
- #error_message_received(message) ⇒ Object
-
#initialize(url, message_dispatcher, connected: nil) ⇒ Relay
constructor
A new instance of Relay.
- #mark_as_offline(uid) ⇒ Object
-
#process_error(message) ⇒ Object
TODO: what does an error message look like? TODO: what are situations in which we receive an error message?.
-
#process_message(message) ⇒ Object
example messages: “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>“message”=>“hi”} “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>“error”=>“hi”} “identifier”=>“{"channel":"MeshRelayChannel"”, “type”=>“confirm_subscription”} “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>with UID user2 could not be found”}.
- #setup(connected: nil) ⇒ Object
Constructor Details
#initialize(url, message_dispatcher, connected: nil) ⇒ Relay
16 17 18 19 20 21 22 23 24 |
# File 'lib/meshchat/network/remote/relay.rb', line 16 def initialize(url, , connected: nil) @_url = url @_request_processor = Incoming::RequestProcessor.new( network: NETWORK_RELAY, location: url, message_dispatcher: ) setup(connected: connected) end |
Instance Attribute Details
#_client ⇒ Object (readonly)
Returns the value of attribute _client.
12 13 14 |
# File 'lib/meshchat/network/remote/relay.rb', line 12 def _client @_client end |
#_connected ⇒ Object
Returns the value of attribute _connected.
13 14 15 |
# File 'lib/meshchat/network/remote/relay.rb', line 13 def _connected @_connected end |
#_request_processor ⇒ Object (readonly)
Returns the value of attribute _request_processor.
12 13 14 |
# File 'lib/meshchat/network/remote/relay.rb', line 12 def _request_processor @_request_processor end |
#_url ⇒ Object (readonly)
Returns the value of attribute _url.
12 13 14 |
# File 'lib/meshchat/network/remote/relay.rb', line 12 def _url @_url end |
Instance Method Details
#chat_message_received(message) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/meshchat/network/remote/relay.rb', line 88 def () _request_processor.process() rescue => e ap e. puts e.backtrace end |
#connected? ⇒ Boolean
56 57 58 |
# File 'lib/meshchat/network/remote/relay.rb', line 56 def connected? _connected end |
#error_message_received(message) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/meshchat/network/remote/relay.rb', line 95 def () Display.info ['error'] if ['status'] == 404 uid = ['uid'] mark_as_offline(uid) end end |
#mark_as_offline(uid) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/meshchat/network/remote/relay.rb', line 103 def mark_as_offline(uid) node = Node.find_by_uid(uid) if node Display.info "#{node.alias_name} has ventured offline" node.update(on_relay: false) else Display.info 'someone directly sent you a fake offline message' end end |
#process_error(message) ⇒ Object
TODO: what does an error message look like? TODO: what are situations in which we receive an error message?
84 85 86 |
# File 'lib/meshchat/network/remote/relay.rb', line 84 def process_error() Display.alert() end |
#process_message(message) ⇒ Object
example messages: “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>“message”=>“hi”} “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>“error”=>“hi”} “identifier”=>“{"channel":"MeshRelayChannel"”, “type”=>“confirm_subscription”} “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>with UID user2 could not be found”}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/meshchat/network/remote/relay.rb', line 65 def () Debug.(, _url) _, type, = .values_at('identifier', 'type', 'message') # do we want to do anything here? return if type == 'confirm_subscription' # are there any other types of websocket messages? return unless if ['message'] () elsif ['error'] () end end |
#setup(connected: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/meshchat/network/remote/relay.rb', line 26 def setup(connected: nil) path = "#{_url}?uid=#{APP_CONFIG.user['uid']}" @_client = ActionCableClient.new(path, CHANNEL) # don't output anything upon connecting _client.connected { self._connected = true } # If there are errors, report them! _client.errored do || process_error() end _client.subscribed do Debug.subscribed_to_relay connected.call if connected end # forward the encrypted messages to our RequestProcessor # so that they can be decrypted _client.received do || () end _client.disconnected do self._connected = false end _client end |