Class: Wsv::Response::SseBody

Inherits:
Object
  • Object
show all
Defined in:
lib/wsv/response/sse_body.rb

Overview

Body for Server-Sent Events responses. The producer block receives the client socket and writes SSE frames until it returns; the surrounding Connection then closes the TCP connection. Write errors after the peer disconnects (EPIPE, ECONNRESET, IOError) propagate out of #write_to and are swallowed by Connection#write, so producers do not need their own rescue. Producers should ‘io.flush` after each frame to defeat TCP buffering.

‘bytesize` returns 0 because SSE streams have no a-priori known size. AccessLog renders 0 bytes as `-` in Common Log Format.

Instance Method Summary collapse

Constructor Details

#initialize(&producer) ⇒ SseBody

Returns a new instance of SseBody.

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/wsv/response/sse_body.rb', line 16

def initialize(&producer)
  raise ArgumentError, "block required" unless producer

  @producer = producer
end

Instance Method Details

#bytesizeObject



26
27
28
# File 'lib/wsv/response/sse_body.rb', line 26

def bytesize
  0
end

#to_sObject

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/wsv/response/sse_body.rb', line 22

def to_s
  raise NotImplementedError, "SseBody has no static representation; use #write_to(io)"
end

#write_to(io) ⇒ Object



30
31
32
# File 'lib/wsv/response/sse_body.rb', line 30

def write_to(io)
  @producer.call(io)
end