Class: Grumlin::Transport
- Inherits:
-
Object
- Object
- Grumlin::Transport
- Defined in:
- lib/grumlin/transport.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
A transport based on github.com/socketry/async and github.com/socketry/async-websocket.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(url, parent: Async::Task.current, **client_options) ⇒ Transport
constructor
Transport is not reusable.
- #wait ⇒ Object
- #write(message) ⇒ Object
Constructor Details
#initialize(url, parent: Async::Task.current, **client_options) ⇒ Transport
Transport is not reusable. Once closed should be recreated.
10 11 12 13 14 15 16 |
# File 'lib/grumlin/transport.rb', line 10 def initialize(url, parent: Async::Task.current, **) @url = url @parent = parent @client_options = @request_channel = Async::Channel.new @response_channel = Async::Channel.new end |
Instance Attribute Details
#url ⇒ Object (readonly)
A transport based on github.com/socketry/async and github.com/socketry/async-websocket
7 8 9 |
# File 'lib/grumlin/transport.rb', line 7 def url @url end |
Instance Method Details
#close ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/grumlin/transport.rb', line 42 def close return if @closed @closed = true @request_channel.close @response_channel.close begin @connection.close rescue StandardError nil end @connection = nil @request_task&.stop(true) @response_task&.stop(true) end |
#connect ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/grumlin/transport.rb', line 22 def connect raise ClientClosedError if @closed raise AlreadyConnectedError if connected? @connection = Async::WebSocket::Client.connect(Async::HTTP::Endpoint.parse(@url), **@client_options) Console.debug(self) { "Connected to #{@url}." } @response_task = @parent.async { run_response_task } @request_task = @parent.async { run_request_task } @response_channel end |
#connected? ⇒ Boolean
18 19 20 |
# File 'lib/grumlin/transport.rb', line 18 def connected? !@connection.nil? end |
#wait ⇒ Object
61 62 63 64 |
# File 'lib/grumlin/transport.rb', line 61 def wait @request_task.wait @response_task.wait end |
#write(message) ⇒ Object
36 37 38 39 40 |
# File 'lib/grumlin/transport.rb', line 36 def write() raise NotConnectedError unless connected? @request_channel << end |