Sha256: 07af98c2e8167adbf63510f0302c3d19825ab544a902e9e1e8bb61f48f238123

Contents?: true

Size: 884 Bytes

Versions: 1

Compression:

Stored size: 884 Bytes

Contents

require "webrick"

class SSEEvent
  def initialize(text, type: "message", id: Time.now.to_f)
    @id   = id
    @text = text
    @type = type
  end

  def event
    %(event: #{@type}
id: #{@id}
data: {
data: "id": "#{@id}",
data: "type": "#{@type}",
data: "text": "#{@text}"
data: }

)
  end
end

server = WEBrick::HTTPServer.new Port: 9292

clients = []

server.mount_proc "/" do |_, res|
  r, w = IO.pipe
  clients << w

  res.content_type = "text/event-stream"
  res.body = r
  res.chunked = true
end

server.mount_proc "/omg" do |_, res|
  r, w = IO.pipe
  clients << w

  res.content_type = "text/event-stream"
  res.body = r
  res.chunked = true
end

server.mount_proc "/broadcast" do |_, _|
  clients.each do |client|
    Thread.new do
      client << SSEEvent.new("streaming!").event
    end
  end
end

trap :INT do
  clients.each(&:close)
  server.shutdown
end

server.start

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
servent-0.0.1 hypotesis/emitter.ru