Skip to content

@rmanalan /chat.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
require 'java'
require 'atmosphere-spade-server.jar'
include_class 'javax.servlet.http.HttpServlet'
include_class 'org.atmosphere.cpr.AtmosphereHandler'
include_class 'org.atmosphere.grizzly.AtmosphereSpadeServer'
#setup and start the server
def main
AtmosphereSpadeServer.build("http://localhost:8080/").addAtmosphereHandler("", ChatPage.new()).addAtmosphereHandler("/chat-stream", ChatStream.new()).start
end
#serve the chat page
class ChatPage
include AtmosphereHandler
def onRequest(event)
Kernel.load(__FILE__)
res = event.getResponse()
res.setContentType("text/html")
res.addHeader("Cache-Control", "private")
res.addHeader("Pragma", "no-cache")
File.open(File.dirname(__FILE__)+'/chat.html').each { |line|
res.getWriter().write(line)
}
res.getWriter().flush()
event
end
end
#serve the chat stream and post messages
class ChatStream
include AtmosphereHandler
def onEvent(event)
#reload the source so we can work on it
#Kernel.load(__FILE__)
req = event.getRequest();
res = event.getResponse();
res.setContentType("text/html")
res.addHeader("Cache-Control", "private")
res.addHeader("Pragma", "no-cache")
res.setCharacterEncoding("UTF-8");
if (req.getMethod().upcase === "GET")
# for IE
res.getWriter().write("<!-- Comet is a programming technique that enables web " +
"servers to send data to the client without having any need " +
"for the client to request it. -->\n");
res.getWriter().flush();
event.suspend();
elsif (req.getMethod().upcase === "POST")
message = req.getParameterValues("message")[0].to_s
ip = req.getRemoteAddr().to_s
#if (rand()*3).to_i == 2
# message = message.gsub(/[^aeiou]/, '*')
#end
event.getBroadcaster().broadcast(
"<script>window.parent.say('<small>#{ip} - #{req.getHeader("User-Agent").to_s}:</small><br><b>#{message}</b>')</script>")
res.getWriter().write('success')
res.getWriter().flush();
end
event
end
def onMessage(event)
writer = event.getResponse().getWriter()
writer.write(event.getMessage().to_s.ljust(1*1024))
writer.flush()
event
end
end
if !$once
main
$once = true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.