Class: Grumlin::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/grumlin/transport.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **client_options)
  @url = url
  @parent = parent
  @client_options = client_options
  @request_channel = Async::Channel.new
  @response_channel = Async::Channel.new
end

Instance Attribute Details

#urlObject (readonly)



7
8
9
# File 'lib/grumlin/transport.rb', line 7

def url
  @url
end

Instance Method Details

#closeObject



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

#connectObject

Raises:



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

Returns:

  • (Boolean)


18
19
20
# File 'lib/grumlin/transport.rb', line 18

def connected?
  !@connection.nil?
end

#waitObject



61
62
63
64
# File 'lib/grumlin/transport.rb', line 61

def wait
  @request_task.wait
  @response_task.wait
end

#write(message) ⇒ Object

Raises:



36
37
38
39
40
# File 'lib/grumlin/transport.rb', line 36

def write(message)
  raise NotConnectedError unless connected?

  @request_channel << message
end