Public Instance methods
each_message(&block)
yields each event Message as the server emits them.
[show source]
# File lib/httpx/plugins/server_sent_events.rb 71 def each_message(&block) 72 return enum_for(__method__) unless block 73 74 payload = {} 75 76 each_line do |line| 77 if line.empty? 78 if payload[:comment] 79 payload.clear 80 next 81 end 82 83 next if payload.empty? 84 85 message = Message.new(**payload) 86 87 payload.clear 88 89 @request.last_server_sent_message = message 90 91 yield message 92 else 93 type, value = line.split(": ", 2) 94 95 case type 96 when "data" 97 type = type.to_sym 98 if payload.key?(type) 99 payload[type] << "\n" << value 100 else 101 payload[type] = value 102 end 103 when "id", "event", "retry" 104 type = type.to_sym 105 raise_format_error(line) if payload.key?(type) || value.empty? 106 107 type = :retry_after if type == :retry # avoid using keyword 108 109 payload[type] = value 110 else 111 # skip if it's a comment 112 if line.start_with?(":") 113 payload[:comment] = true 114 next 115 end 116 117 raise_format_error(line) 118 end 119 end 120 end 121 end