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.
-
#_message_queue ⇒ Object
readonly
Returns the value of attribute _message_queue.
-
#_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, subscribed_callback = 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”}.
- #send_now(payload) ⇒ Object
- #setup(subscribed_callback: nil) ⇒ Object
Constructor Details
#initialize(url, message_dispatcher, subscribed_callback = nil) ⇒ Relay
Returns a new instance of Relay.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/meshchat/network/remote/relay.rb', line 18 def initialize(url, , subscribed_callback = nil) @_url = url @_message_queue = [] @_request_processor = Incoming::RequestProcessor.new( network: NETWORK_RELAY, location: url, message_dispatcher: ) setup(subscribed_callback: subscribed_callback) 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 |
#_message_queue ⇒ Object (readonly)
Returns the value of attribute _message_queue.
12 13 14 |
# File 'lib/meshchat/network/remote/relay.rb', line 12 def @_message_queue 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
100 101 102 103 104 105 |
# File 'lib/meshchat/network/remote/relay.rb', line 100 def () _request_processor.process() rescue => e ap e. puts e.backtrace end |
#connected? ⇒ Boolean
68 69 70 |
# File 'lib/meshchat/network/remote/relay.rb', line 68 def connected? _connected end |
#error_message_received(message) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/meshchat/network/remote/relay.rb', line 107 def () Display.info ['error'] if ['status'] == 404 uid = ['uid'] mark_as_offline(uid) end end |
#mark_as_offline(uid) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/meshchat/network/remote/relay.rb', line 115 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?
96 97 98 |
# File 'lib/meshchat/network/remote/relay.rb', line 96 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”}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/meshchat/network/remote/relay.rb', line 77 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 |
#send_now(payload) ⇒ Object
64 65 66 |
# File 'lib/meshchat/network/remote/relay.rb', line 64 def send_now(payload) _client.perform('chat', payload) end |
#setup(subscribed_callback: nil) ⇒ Object
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 55 56 57 58 59 60 61 62 |
# File 'lib/meshchat/network/remote/relay.rb', line 30 def setup(subscribed_callback: nil) path = "#{_url}?uid=#{APP_CONFIG.user['uid']}" @_client = ActionCableClient.new(path, CHANNEL) # don't output anything upon connecting _client.connected do Debug.connected_to_relay self._connected = true end # If there are errors, report them! _client.errored do || process_error() end _client.subscribed do Debug.subscribed_to_relay subscribed_callback.call if subscribed_callback end # forward the encrypted messages to our RequestProcessor # so that they can be decrypted _client.received do || () end _client.disconnected do Debug.disconnected_from_relay self._connected = false end _client end |