class NchanTools::Subscriber::EventSourceClient
Public Class Methods
aliases()
click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1158 def self.aliases [:eventsource, :sse] end
Public Instance Methods
error(c,m,cn=nil)
click to toggle source
Calls superclass method
NchanTools::Subscriber::LongPollClient#error
# File lib/nchan_tools/pubsub.rb, line 1162 def error(c,m,cn=nil) @error_what ||= [ "#{@http2 ? 'HTTP/2' : 'HTTP'} Request failed", "connection closed" ] @error_failword ||= "" super end
new_bundle(uri, opt={})
click to toggle source
Calls superclass method
NchanTools::Subscriber::LongPollClient#new_bundle
# File lib/nchan_tools/pubsub.rb, line 1224 def new_bundle(uri, opt={}) opt[:accept]="text/event-stream" super end
setup_bundle(b)
click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1229 def setup_bundle(b) b.on_headers do |code, headers| if code == 200 @notready-=1 @cooked_ready.signal true if @notready == 0 b.connected = true end end b.buffer_body! b.subparser=EventSourceParser.new b.on_chunk do |chunk| while b.body_buf.slice! /^.*\n/ do b.subparser.parse_line $~[0] end end b.on_error do |msg, err| if EOFError === err && !b.subparser.buf_empty? b.subparser.parse_line "\n" end handle_bundle_error b, msg, err end b.on_response do |code, headers, body| if code != 200 @subscriber.on_failure error(code, "", b) @subscriber.finished+=1 else if !b.subparser.buf_empty? b.subparser.parse_line "\n" else @subscriber.on_failure error(0, "Response completed unexpectedly", b) end @subscriber.finished+=1 end close b end b.subparser.on_event do |evt, data, evt_id| case evt when :comment if data.match(/^(?<code>\d+): (?<message>.*)/) @subscriber.on_failure error($~[:code].to_i, $~[:message], b) @subscriber.finished+=1 close b end else @timer.reset if @timer unless @nomsg msg=Message.new data.dup msg.id=evt_id msg.eventsource_event=evt else msg=data end if @subscriber.on_message(msg, b) == false @subscriber.finished+=1 close b end end end b end