#!/usr/bin/env ruby -I ../lib -I lib # coding: utf-8 require 'sinatra' set :server, 'thin' connections = [] get '/' do halt erb(:login) unless params[:user] erb :chat, :locals => { :user => params[:user].gsub(/\W/, '') } end get '/stream', :provides => 'text/event-stream' do stream :keep_open do |out| connections << out out.callback { connections.delete(out) } end end post '/' do connections.each { |out| out << "data: #{params[:msg]}\n\n" } 204 # response without entity body end __END__ @@ layout