small pixel drawing of a pufferfish shitchat

initial commit
root root@j3s.sh
Wed, 20 May 2020 03:01:29 +0000
commit

c1466a6c660a04bd3e552e832b0ec94761cb170e

3 files changed, 111 insertions(+), 0 deletions(-)

jump to
A Gemfile

@@ -0,0 +1,5 @@

+source 'https://rubygems.org' + +gem 'sinatra' + +gem 'thin'
A Gemfile.lock

@@ -0,0 +1,31 @@

+GEM + remote: https://rubygems.org/ + specs: + daemons (1.3.1) + eventmachine (1.2.7) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + rack (2.2.2) + rack-protection (2.0.8.1) + rack + ruby2_keywords (0.0.2) + sinatra (2.0.8.1) + mustermann (~> 1.0) + rack (~> 2.0) + rack-protection (= 2.0.8.1) + tilt (~> 2.0) + thin (1.7.2) + daemons (~> 1.0, >= 1.0.9) + eventmachine (~> 1.0, >= 1.0.4) + rack (>= 1, < 3) + tilt (2.0.10) + +PLATFORMS + ruby + +DEPENDENCIES + sinatra + thin + +BUNDLED WITH + 2.1.4
A app.rb

@@ -0,0 +1,75 @@

+#!/usr/bin/env ruby -I ../lib -I lib +# coding: utf-8 +require 'sinatra' +set :server, 'thin' +connections = [] +set :bind, '0.0.0.0' + +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>shitchat</title> + <meta charset="utf-8" /> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> + <style> + body { + color: #c5c8c6; + font: monospace; + text-align: left; overflow-y: hidden; + background-color: #241e1e; } + #chat { + height: 50vh; + overflow-y: hidden; + display: flex; + flex-direction: column-reverse; + } + #msg { width: 100% }; + </style> + </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').prepend('<span>' + e.data + '<span>' ) }; + + // writing + $("form").on('submit', function(e) { + $.post('/', {msg: "<%= user %>: " + $('#msg').val()}); + $('#msg').val(''); $('#msg').focus(); + e.preventDefault(); + }); +</script>