Skip to content

Commit

Permalink
Fixing bugs for the new version. Adding Bundler support
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaonrails committed Feb 15, 2011
1 parent 7e2f8d3 commit 98153ed
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 113 deletions.
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'http://rubygems.org'

gem 'cramp', '~> 0.12'
gem 'tramp', '~> 0.1'

gem 'activesupport', '3.0.4'
gem 'rack', '~> 1.2.1'
gem 'eventmachine', '~> 0.12.10'

gem 'usher', '~> 0.8.3'
gem 'thin', '~> 1.2.7'
44 changes: 44 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.4)
activesupport (= 3.0.4)
builder (~> 2.1.2)
i18n (~> 0.4)
activesupport (3.0.4)
arel (0.4.0)
activesupport (>= 3.0.0.beta)
builder (2.1.2)
cramp (0.12)
activesupport (~> 3.0.4)
eventmachine (~> 0.12.10)
rack (~> 1.2.1)
daemons (1.1.0)
eventmachine (0.12.10)
fuzzyhash (0.0.11)
i18n (0.5.0)
mysqlplus (0.1.2)
rack (1.2.1)
thin (1.2.7)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tramp (0.1)
activemodel (~> 3.0.0.beta4)
activesupport (~> 3.0.0.beta4)
arel (~> 0.4.0)
mysqlplus (~> 0.1.1)
usher (0.8.3)
fuzzyhash (>= 0.0.11)

PLATFORMS
ruby

DEPENDENCIES
activesupport (= 3.0.4)
cramp (~> 0.12)
eventmachine (~> 0.12.10)
rack (~> 1.2.1)
thin (~> 1.2.7)
tramp (~> 0.1)
usher (~> 0.8.3)
4 changes: 1 addition & 3 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ h2. Usage
This should install all the dependencies

<pre>
gem install thin
gem install cramp
gem install tramp
bundle install
</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.
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/chat_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ def retrieve_messages
@last_message ||= (Time.now - 1.minute)
Chat.recent(@last_message).all do |messages|
list = messages.map { |msg| { "from" => msg.name, "msg" => msg.message, "sent" => msg.sent_at.to_formatted_s(:short) } }
@last_message = messages.first.try(:sent_at) || @last_message
render [list.to_json, "\n"]
if list.size > 0
@last_message = messages.first.try(:sent_at) || @last_message
render list.to_json
end
end
end

Expand All @@ -26,4 +28,4 @@ def receive_message(data)
end
end
end
end
end
110 changes: 55 additions & 55 deletions app/views/chat.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<html lang="en">
<head>
<title>chat</title>
<style type="text/css" media="screen">
#messages {
width: 400px;
height: 250px;
overflow: auto;
}
</style>
<style type="text/css" media="screen">
#messages {
width: 400px;
height: 250px;
overflow: auto;
}
</style>
</head>

<body>
Expand All @@ -33,59 +33,59 @@

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="javascript/swfobject.js"></script>
<script type="text/javascript" src="javascript/FABridge.js"></script>
<script type="text/javascript" src="javascript/web_socket.js"></script>
<script type="text/javascript">
<script type="text/javascript" src="javascript/swfobject.js"></script>
<script type="text/javascript" src="javascript/FABridge.js"></script>
<script type="text/javascript" src="javascript/web_socket.js"></script>
<script type="text/javascript">

// Set URL of your WebSocketMain.swf here:
WebSocket.__swfLocation = "javascript/WebSocketMain.swf";
// Set URL of your WebSocketMain.swf here:
WebSocket.__swfLocation = "javascript/WebSocketMain.swf";

var ws;
var ws;

function initConnection() {
// Connect to Web Socket.
// Change host/port here to your own Web Socket server.
ws = new WebSocket("ws://<%= request.host_with_port %>/websocket");
function initConnection() {
// Connect to Web Socket.
// Change host/port here to your own Web Socket server.
ws = new WebSocket("ws://<%= request.host_with_port %>/websocket");

// Set event handlers.
ws.onopen = function() {
output([{message: "Connection opened"}]);
};
ws.onmessage = function(e) {
// e.data contains received string.
output(eval(e.data));
};
ws.onclose = function() {
output([{message: "Connection closed"}]);
initConnection(); // reconnect
};
}
$(document).ready(function() {
initConnection();
$("#msg").focus();
});
// Set event handlers.
ws.onopen = function() {
output([{message: "Connection opened"}]);
};
ws.onmessage = function(e) {
// e.data contains received string.
output(eval(e.data));
};
ws.onclose = function() {
output([{message: "Connection closed"}]);
initConnection(); // reconnect
};
}
$(document).ready(function() {
initConnection();
$("#msg").focus();
});

function onSubmit() {
ws.send($("#chat-form").serialize());
$("#msg").val("");
$("#msg").focus();
}
function onSubmit() {
ws.send($("#chat-form").serialize());
$("#msg").val("");
$("#msg").focus();
}

function output(data) {
$.each(data, function(i, item) {
if (item.from != null) {
$("#messages").append( "<div><span class=\"from\">" + item.from + ":</span>\n<span class=\"msg\">" + item.msg + "</span></div>");
}
if (item.message != null) {
$("#messages").append( "<p>" + item.message + "</p>");
}
})
var messages = document.getElementById("messages");
messages.scrollTop = messages.scrollHeight;
}
function output(data) {
$.each(data, function(i, item) {
if (item.from != null) {
$("#messages").append( "<div><span class=\"from\">" + item.from + ":</span>\n<span class=\"msg\">" + item.msg + "</span></div>");
}
if (item.message != null) {
$("#messages").append( "<p>" + item.message + "</p>");
}
})
var messages = document.getElementById("messages");
messages.scrollTop = messages.scrollHeight;
}

</script>
</script>
</body>
</html>
</html>
100 changes: 50 additions & 50 deletions app/views/chat_poll.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<html lang="en">
<head>
<title>chat</title>
<style type="text/css" media="screen">
#messages {
width: 400px;
height: 250px;
overflow: auto;
}
</style>
<style type="text/css" media="screen">
#messages {
width: 400px;
height: 250px;
overflow: auto;
}
</style>
</head>

<body>
Expand All @@ -33,50 +33,50 @@

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
var lastTimestamp = {};
// method used to update element html
$(document).ready(function() {
$("#msg").focus();
setInterval(function() {
$.ajax({
url: "http://<%= request.host_with_port %>/retrieve",
type: "GET",
data: lastTimestamp,
dataType: "json",
success: function(data) {
$.each(data, function(i, item) {
$("#messages").append( "<div><span class=\"from\">" + item.from + ":</span>\n<span class=\"msg\">" + item.msg + "</span></div>");
lastTimestamp = { last_timestamp: item.timestamp }
});
scrollDown();
},
})
}, 5000);
});
<script type="text/javascript">
var lastTimestamp = {};
// method used to update element html
$(document).ready(function() {
$("#msg").focus();
setInterval(function() {
$.ajax({
url: "http://<%= request.host_with_port %>/retrieve",
type: "GET",
data: lastTimestamp,
dataType: "json",
success: function(data) {
$.each(data, function(i, item) {
$("#messages").append( "<div><span class=\"from\">" + item.from + ":</span>\n<span class=\"msg\">" + item.msg + "</span></div>");
lastTimestamp = { last_timestamp: item.timestamp }
});
scrollDown();
},
})
}, 1000);
});

function onSubmit() {
$.ajax({
url: "http://<%= request.host_with_port %>/receive",
type: "POST",
dataType: "json",
data: $("#chat-form").serialize(),
success: function(data) {
$.each(data, function(i, item){
$("#messages").append( "<p>" + item.message + "</p>");
})
scrollDown();
},
});
$("#msg").val("").focus();
}
function onSubmit() {
$.ajax({
url: "http://<%= request.host_with_port %>/receive",
type: "POST",
dataType: "json",
data: $("#chat-form").serialize(),
success: function(data) {
$.each(data, function(i, item){
$("#messages").append( "<p>" + item.message + "</p>");
})
scrollDown();
},
});
$("#msg").val("").focus();
}

function scrollDown() {
var messages = document.getElementById("messages");
messages.scrollTop = messages.scrollHeight;
}
function scrollDown() {
var messages = document.getElementById("messages");
messages.scrollTop = messages.scrollHeight;
}

</script>
</script>
</body>
</html>
</html>
6 changes: 4 additions & 2 deletions chatserver.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'rubygems'
require 'thin'
require 'bundler'
Bundler.setup(:default)

require 'usher'
require 'erb'
require 'thin'
require 'cramp'
require 'tramp'
require 'active_support/all'
Expand Down

0 comments on commit 98153ed

Please sign in to comment.