Update streaming example

This commit is contained in:
Jordan Owens 2020-08-31 14:22:44 -04:00
parent 4b502e0ec6
commit 0624b13a68
1 changed files with 4 additions and 5 deletions

View File

@ -1695,7 +1695,7 @@ require 'sinatra/base'
class App < Sinatra::Base
connections = []
get '/subscribe' do
get '/subscribe', provides: 'text/event-stream' do
# register a client's interest in server events
stream(:keep_open) do |out|
connections << out
@ -1704,17 +1704,16 @@ class App < Sinatra::Base
end
end
post '/:message' do
post '/' do
connections.each do |out|
# notify client that a new message has arrived
out << params['message'] << "\n"
out << "data: #{params[:msg]}\n\n"
# indicate client to connect again
out.close
end
# acknowledge
"message received"
204 # response without entity body
end
end