用 AjaxMessaging 在五分鐘內造一個 chat room
ajaxmessaging, jms, rails, ruby at July 8th, 2007 by 小影
English version: Using AjaxMessaging to create a chatroom in 5 mins
把之前一篇的源碼重整理了一下,作了這個 plugin。
1. 準備
- 下載和安裝 Ruby on Rails
- 下載和安裝 Apache ActiveMQ
- 安裝 Mongrel ( sudo gem install mongrel )
- 安裝 json (sudo gem install json)
- 安裝 stomp (sudo gem install stomp)
2. 產生 chat room 軟件
rails chat
3. 安裝 AjaxMessaging plugin
cd chat
script/plugin install http://ajaxmessaging.googlecode.com/svn/trunk/plugins/ajaxmessaging
4. 設定
a. 設定 Apache ActiveMQ (topic ‘chat’)
ACTIVEMQ_HOME/conf/activemq.xml
{[.activemq-xml /enzymes/xml.php]}
b. 設定 AjaxMessaging (我們用預設值就可以了)
config/a11g.yml
{[.a11g-yml /enzymes/ruby.php]}
c. 設定資料庫 (我們用 sqlite3)
config/database.yml
{[.database-yml /enzymes/ruby.php]}
d. 設定 Mongrel
config/mongrel_conf.yml
:config_script: lib/ajax_messaging_handler.rb
5. 準備 Data Model 和 Scaffold
./script/generate scaffold_resource chat name:string message:string
rake db:migrate
6. 修改 Chat Layout
app/view/layouts/chats.rhtml
{[.chats-rhtml /enzymes/html.php]}
7. 修改 index view
app/view/chats/index.rhtml
{[.index-rhtml /enzymes/html.php]}
8. 修改 new view
app/views/chats/new.rhtml
{[.new-rhtml /enzymes/html.php]}
9. 修改 show view 去顯示訊息
app/views/chats/show.rhtml
{[.show-rhtml /enzymes/html.php]}
10. 修改 chat controller
{[.chats_controller-rb /enzymes/ruby.php]}
11. 啟動伺服器
a. 啟動 ActiveMQ :
cd apache-activemq
./bin/activemq
b. 啟動 Mongrel
cd chat
mongrel_rails start –C ./config/mongrel_conf.rb
c. 開始 Chat
開啟瀏覽器瀏覽http://localhost:3000/chats,你的訊息會即時被其他在這 chat room的人看到。注意:有些瀏覽器限制了同一個網站的連線數目 (如 Firefox) ,想在同一電腦跟自己 chat ,請用兩個不同的 browser。
Credit: 這個教學來自 shooting_star 的相同教學。如果你需要完整、強大又穩定的 comet server,大概你需要的是 shooting_star。
相關連結:
- 下載以上的源碼
- 有關這個 chat room 的原理,請讀Ruby on Rails + AJAX + Mongrel + JMS/MQ/MOM = 即時通訊
- AjaxMessaging Project Home Page
