Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing bugs for the new version. Adding Bundler support

  • Loading branch information...
commit 98153eda727730dfb05b4110f498ace6ecd2eee4 1 parent 7e2f8d3
@akitaonrails akitaonrails authored
View
11 Gemfile
@@ -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'
View
44 Gemfile.lock
@@ -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 README.textile
@@ -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.
View
8 app/controllers/chat_controller.rb
@@ -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
@@ -26,4 +28,4 @@ def receive_message(data)
end
end
end
-end
+end
View
110 app/views/chat.erb
@@ -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>
@@ -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>
View
100 app/views/chat_poll.erb
@@ -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>
@@ -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>
View
6 chatserver.rb
@@ -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'
Please sign in to comment.
Something went wrong with that request. Please try again.