Class: RubyLsp::MessageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/utils.rb

Overview

Reads JSON RPC messages from the given IO in a loop

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ MessageReader

: (IO) -> void



312
313
314
# File 'lib/ruby_lsp/utils.rb', line 312

def initialize(io)
  @io = io
end

Instance Method Details

#each_message(&block) ⇒ Object

: () { (Hash[Symbol, untyped]) -> void } -> void



317
318
319
320
321
322
# File 'lib/ruby_lsp/utils.rb', line 317

def each_message(&block)
  while (headers = @io.gets("\r\n\r\n"))
    raw_message = @io.read(headers[/Content-Length: (\d+)/i, 1].to_i) #: as !nil
    block.call(JSON.parse(raw_message, symbolize_names: true))
  end
end