Sha256: 90a5832e3f1d6f18329e0f168f238b7cb7ed573e5c081895986359783f0ac8b5

Contents?: true

Size: 1.3 KB

Versions: 95

Compression:

Stored size: 1.3 KB

Contents

#!/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
<html>
  <head>
    <title>Super Simple Chat with Sinatra</title>
    <meta charset="utf-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  </head>
  <body><%= yield %></body>
</html>

@@ login
<form action='/'>
  <label for='user'>User Name:</label>
  <input name='user' value='' />
  <input type='submit' value="GO!" />
</form>

@@ chat
<pre id='chat'></pre>
<form>
  <input id='msg' placeholder='type message here...' />
</form>

<script>
  // reading
  var es = new EventSource('/stream');
  es.onmessage = function(e) { $('#chat').append(e.data + "\n") };

  // writing
  $("form").on('submit',function(e) {
    $.post('/', {msg: "<%= user %>: " + $('#msg').val()});
    $('#msg').val(''); $('#msg').focus();
    e.preventDefault();
  });
</script>

Version data entries

95 entries across 91 versions & 20 rubygems

Version Path
logstash-output-scalyr-0.1.9 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.8 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.7 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.6 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.3.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.3.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/sinatra-1.4.8/examples/chat.rb
sinatra-2.0.8.1 examples/chat.rb
sinatra-2.0.8 examples/chat.rb
sinatra-2.0.7 examples/chat.rb
sinatra-2.0.6 examples/chat.rb
sinatra-2.0.5 examples/chat.rb
sinatra-2.0.4 examples/chat.rb
logstash-filter-device_detection-1.0.7-java vendor/bundle/jruby/1.9/gems/sinatra-1.4.8/examples/chat.rb