Class: Wamp::Router::Connection
- Includes:
- WebSocket::Driver::EventEmitter
- Defined in:
- lib/wamp/router/connection.rb
Overview
TOP Level Doc
Constant Summary collapse
- CONNECTING =
0- OPEN =
1- CLOSING =
2- CLOSED =
3- SUPPORTED_PROTOCOLS =
["wamp.2.msgpack", "wamp.2.cbor", "wamp.2.json"].freeze
Instance Attribute Summary collapse
-
#acceptor ⇒ Object
readonly
Returns the value of attribute acceptor.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Attributes inherited from Client
Instance Method Summary collapse
- #begin_close(reason, code) ⇒ Object
- #connection ⇒ Object
- #finalize_close ⇒ Object
-
#initialize(socket, &cleanup) ⇒ Connection
constructor
rubocop:disable Lint/MissingSuper.
-
#listen(&block) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #on_connect ⇒ Object
-
#receive_data(data) ⇒ Object
triggers on_message.
- #transmit(message) ⇒ Object
-
#write(data) ⇒ Object
called when @driver.text is invoked.
Methods inherited from Client
#on_message, #realm, #send_message, #session_id
Constructor Details
#initialize(socket, &cleanup) ⇒ Connection
rubocop:disable Lint/MissingSuper
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/wamp/router/connection.rb', line 25 def initialize(socket, &cleanup) # rubocop:disable Lint/MissingSuper # super() # on_connect() does what super() does @cleanup = cleanup @socket = socket @driver = WebSocket::Driver.server(self, protocols: SUPPORTED_PROTOCOLS) @driver.on(:open) { on_open(_1) } @driver.on(:message) { (_1.data) } @driver.on(:close) { |evt| begin_close(evt.reason, evt.code) } @driver.on(:connect) { on_connect } @ready_state = CONNECTING end |
Instance Attribute Details
#acceptor ⇒ Object (readonly)
Returns the value of attribute acceptor.
23 24 25 |
# File 'lib/wamp/router/connection.rb', line 23 def acceptor @acceptor end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
23 24 25 |
# File 'lib/wamp/router/connection.rb', line 23 def session @session end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
23 24 25 |
# File 'lib/wamp/router/connection.rb', line 23 def socket @socket end |
Instance Method Details
#begin_close(reason, code) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/wamp/router/connection.rb', line 47 def begin_close(reason, code) return if @ready_state == CLOSED @ready_state = CLOSING @close_params = [reason, code] @cleanup&.call(self) finalize_close end |
#connection ⇒ Object
43 44 45 |
# File 'lib/wamp/router/connection.rb', line 43 def connection self end |
#finalize_close ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/wamp/router/connection.rb', line 57 def finalize_close return if @ready_state == CLOSED @ready_state = CLOSED @driver.close(*@close_params) socket.close end |
#listen(&block) ⇒ Object
rubocop:disable Metrics/MethodLength
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/wamp/router/connection.rb', line 65 def listen(&block) # rubocop:disable Metrics/MethodLength return unless [CONNECTING, OPEN].include?(@ready_state) begin data = socket.read_nonblock(4096, exception: false) case data when :wait_readable # do nothing when nil block&.call @driver.close else receive_data(data) end rescue StandardError => e puts e. puts e.backtrace begin block&.call @driver.close rescue StandardError # Errno::ECONNRESET puts "Failed to handle: Errno::ECONNRESET" end end end |
#on_connect ⇒ Object
37 38 39 40 41 |
# File 'lib/wamp/router/connection.rb', line 37 def on_connect @driver.start if WebSocket::Driver.websocket?(@driver.env) choose_serializer_from @driver.env["HTTP_SEC_WEBSOCKET_PROTOCOL"] @acceptor = Wampproto::Acceptor.new(serializer, Authenticator) end |
#receive_data(data) ⇒ Object
triggers on_message
93 94 95 96 97 |
# File 'lib/wamp/router/connection.rb', line 93 def receive_data(data) return unless [OPEN, CONNECTING].include?(@ready_state) @driver.parse(data) end |
#transmit(message) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/wamp/router/connection.rb', line 104 def transmit() # return false if @ready_state > OPEN case when Wampproto::Message::Base then transmit(serializer.serialize()) when Numeric then @driver.text(.to_s) when String then @driver.text() when Array then @driver.binary() else false end end |
#write(data) ⇒ Object
called when @driver.text is invoked
100 101 102 |
# File 'lib/wamp/router/connection.rb', line 100 def write(data) socket.write(data) end |