Skip to content
Browse files

Get rid of static files and use erb for dynamic views. Also dont forc…

…e foo.com.br.
  • Loading branch information...
1 parent 52de0c0 commit 510220cda7782517000fe3330dab900d890a4a44 @lifo committed Jan 12, 2010
View
16 README.textile
@@ -15,12 +15,6 @@ This should install all the dependencies
gem install cramp
</pre>
-You will need a valid hostname if you're going to test it under your machine so edit your @/etc/hosts@ and add:
-
-<pre>
- 127.0.0.1 localhost foo.com.br
-</pre>
-
This demo uses MySQL, so you will need to create your own database. Read @config/database.sql@ to create it and edit @config/database.yml@ with the correct server, username and password.
If you want to test the WebSocket implementation outside of Google Chrome 4+, the app loads "web-socket.js":http://github.com/gimite/web-socket-js. As it uses Flash connection it is required that the server provides a policy file. This app uses "Peleus Uhley":http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html implementation available in the @extra@ folder. Start it like this:
@@ -39,18 +33,18 @@ It has to be @sudo@ because 843 is a privileged port. Now you need to start up t
It will start the server on the port 3000. So you can fire up a webbrowser and use either of these URLs:
<pre>
- http://foo.com.br:3000/public/chat.html
- http://foo.com.br:3000/public/chat_poll.html
+ http://0.0.0.0:3000/chat
+ http://0.0.0.0:3000/chat_poll
</pre>
The first URL is the WebSocket version. The second is a more usual Ajax Polling version, which may be more compatible with webbrowsers. As the WebSocket version uses Flash it's less friendly to environments behind proxies and firewalls.
You can also test the server using @curl@
<pre>
- curl http://foo.com.br:3000/websocket
- curl http://foo.com.br:3000/retrieve
- curl -d "from=foo&msg=hello" http://foo.com.br:3000/receive
+ curl http://0.0.0.0:3000/websocket
+ curl http://0.0.0.0:3000/retrieve
+ curl -d "from=foo&msg=hello" http://0.0.0.0:3000/receive
</pre>
The first URL will keep an open HTTP connection, receiving new messages as they are inserted in the database. The second one retrieves the last few messages and you can pass a 'last_timestamp' parameter to the URL with the value having the format of @Time.now.to_formatted_s(:db)@. Finally, the third operation simulates a form post to add new messages. The first URL is the WebSocket version, the last 2 are the polling version.
View
29 app/controllers/static_controller.rb
@@ -0,0 +1,29 @@
+class StaticController < ApplicationController
+ @@views = {}
+
+ Dir[File.join(File.dirname(__FILE__), "../views/*.erb")].each do |f|
+ @@views[File.basename(f, '.erb')] = ERB.new(File.read(f))
+ end
+
+ before_start :check_file_exists
+
+ def check_file_exists
+ file = params[:file].present? ? params[:file] : 'index'
+
+ if @@views.has_key?(file)
+ @template = @@views[file]
+ yield
+ else
+ halt 404
+ end
+ end
+
+ def respond_with
+ [200, {'Content-Type' => 'text/html'}]
+ end
+
+ def start
+ render @template.result(binding)
+ finish
+ end
+end
View
2 public/chat.html → app/views/chat.erb
@@ -46,7 +46,7 @@
function initConnection() {
// Connect to Web Socket.
// Change host/port here to your own Web Socket server.
- ws = new WebSocket("ws://foo.com.br:3000/websocket");
+ ws = new WebSocket("ws://<%= request.host_with_port %>/websocket");
// Set event handlers.
ws.onopen = function() {
View
4 public/chat_poll.html → app/views/chat_poll.erb
@@ -41,7 +41,7 @@
$("#msg").focus();
setInterval(function() {
$.ajax({
- url: "http://foo.com.br:3000/retrieve",
+ url: "http://<%= request.host_with_port %>/retrieve",
type: "GET",
data: lastTimestamp,
dataType: "json",
@@ -58,7 +58,7 @@
function onSubmit() {
$.ajax({
- url: "http://foo.com.br:3000/receive",
+ url: "http://<%= request.host_with_port %>/receive",
type: "POST",
dataType: "json",
data: $("#chat-form").serialize(),
View
1 app/views/index.erb
@@ -0,0 +1 @@
+Hello World!!
View
2 chatserver.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'thin'
require 'usher'
+require 'erb'
require 'cramp/controller'
require 'cramp/model'
require 'active_support/all'
@@ -12,6 +13,7 @@
require 'app/controllers/chat_controller'
require 'app/controllers/retrieve_controller'
require 'app/controllers/receive_controller'
+require 'app/controllers/static_controller'
require 'app/models/chat'
require 'lib/fixes'
View
1 config/routes.rb
@@ -7,6 +7,7 @@ def app_routes
get('/websocket').to(ChatController)
get('/retrieve').to(RetrieveController)
post('/receive').to(ReceiveController)
+ get('/(:file)').to(StaticController)
end
run routes
end

0 comments on commit 510220c

Please sign in to comment.
Something went wrong with that request. Please try again.