This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/70

Juggernautは、Ajaxianとかでも紹介されていましたが、Comet for Rails?なRailsプラグインです。 Flashのxmlsocketを使って、サーバとブラウザ間にコネクションを張り、リアルタイムにページの更新を行ったりする事ができます。

moroさんの記事で紹介されていたRails Chatでも利用されているようです。

まずはインストールしてみましょう。JuggernautはJsonライブラリに依存しているので、無ければ先にインストールします。

# gem install json
さて、続いてJuggernaut本体のインストールです。
$ ./script/plugin install \
svn://rubyforge.org/var/svn/juggernaut/trunk/juggernaut
$ rake install_juggernaut
$ ./script/plugin install \
svn://rubyforge.org/var/svn/juggernaut/trunk\
/juggernaut/vendor/plugins/juggernaut
これで完了です。毎度の事ながら、Railsのプラグイン機構は本当に便利ですね。 (注: 8月30日現在、インストール方法が上記のように変わっているようです)

さて、続いてrhtmlファイルからJuggernautのjavascriptを呼び出すようにします。

<%= javascript_include_tag "juggernaut_javascript" %>
<%= javascript_include_tag "juggernaut" %>
app/views/layouts/*.rhtmlあたりに記述しておけば大丈夫でしょう。 さらに、Juggernautが使用するFlashもbodyの最後あたりに埋め込みます。
<%= flash_plugin(session[:juggernaut_channels]) %>
最後の仕上げに、Juggernautの設定ファイルの雛形がconfig/juggernaut_config.yml に作成されているので、適宜変更してください。

これでJuggernautを使う準備は整いました。 実際に簡単なChatアプリケーションを作ってみましょう。

$ rails chat
$ cd chat
$ ./script/generate controller chat
ChatControllerとViewを記述します。
$ vim app/controllers/chat_controller.rb
class ChatController < ApplicationController
  def index
    session[:juggernaut_channels] = ["chat_channel"]
  end

  def send_data
    input_data = Juggernaut.html_and_string_escape(params[:chat_input])
    data = "new Insertion.Top('chat_data', '<li>#{ input_data }</li>');"
    Juggernaut.send(data, session[:juggernaut_channels])
    render :nothing => "true"
  end
end
続いてView
$ vim app/views/chat/index.rhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Chat</title>
  <%= stylesheet_link_tag 'default' %>
  <%= javascript_include_tag :defaults %>
  <%= javascript_include_tag "juggernaut_javascript" %>
</head>
<body>
  <%= form_remote_tag(
    :url => {:action => :send_data},
    :complete => "$('chat_input').value = ''") %>
    <%= text_field_tag('chat_input', '', {:size => 20, :id => 'chat_input'}) %>
    <%= submit_tag "Add" %>
  <%= end_form_tag %>
  <ul id="chat_data" style="list-style:none">
  </ul>
  <%= flash_plugin(session[:juggernaut_channels]) %>
</body>
</html>
これで完了です。あとは、pushサーバとWebサーバを起動すればOK。
$ ruby script/push_server &
$ ./script/server
http://localhost:3000/chat/にアクセスしてみましょう。
リアルタイムに書き込んだ内容が反映されます。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/70

posted by genki genki on Sun 23 Jul 2006 at 07:56 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://reviewgoodman.tistory.com/entry/%EB%B0%94%EC%9D%B4%EB%82%B8%EC%8A%A4-%EA%B0%80%EC%9E%85-%EB%B0%A9%EB%B2%95%EB%B6%80%ED%84%B0-%EC%88%98%EC%88%98%EB%A3%8C-%ED%95%A0%EC%9D%B8-%EB%B0%9B%EB%8A%94-%EB%B0%A9%EB%B2%95%EA%B9%8C%EC%A7%80 https://reviewgoodman.tistory.com/entry/%EB%B0%94%EC%9D%B4%EB%82%B8%EC%8A%A4-%EA%B0%80%EC%9E%85-%EB%B0%A9%EB%B2%95%EB%B6%80%ED%84%B0-%EC%88%98%EC%88%98%EB%A3%8C-%ED%95%A0%EC%9D%B8-%EB%B0%9B%EB%8A%94-%EB%B0%A9%EB%B2%95%EA%B9%8C%EC%A7%80: Greate pieces. Keep writing such kind of info on y... 05/22 14:11
https://laviesound.com/denishallanes1 https://laviesound.com/denishallanes1: References: King casino https://laviesound.c... 05/22 14:11
https://www.studiomangili.com/best-online-pokies-for-real-money-in-australia-2026/ https://www.studiomangili.com/best-online-pokies-for-real-money-in-australia-2026/: References: Slot machine android 05/22 14:04
https://itprimejobs.com/employer/highest-payout-online-casinos-in-australia-best-paying-sites2026/ https://itprimejobs.com/employer/highest-payout-online-casinos-in-australia-best-paying-sites2026/: References: Craps board 05/22 14:04
https://adufoshi.com/latashavallejo https://adufoshi.com/latashavallejo: References: 888 casino review https://adufos... 05/22 13:47
https://angdesh.com/author/madget11551/ https://angdesh.com/author/madget11551/: References: Casino dice games 05/22 12:30
https://jobcopae.com/employer/real-money-pokies-australia-play-online-pokies-for-real-cash-in-2026/ https://jobcopae.com/employer/real-money-pokies-australia-play-online-pokies-for-real-cash-in-2026/: References: Hinckley grand casino 05/22 12:23
https://interior01.netpro.co.kr:443/bbs/board.php?bo_table=free&wr_id=336 https://interior01.netpro.co.kr:443/bbs/board.php?bo_table=free&wr_id=336: References: Las vegas casino online 05/22 12:05
Services from s21g
YOMU Web小説リーダー
小説投稿サイトの公式ページを開き、WebView表示と読み上げ向け表示を切り替えて、移動中や作業中にもWeb小説の続きを聴きやすくするiPhoneアプリです。
補助探
公開されている補助金・助成金情報を集約し、条件に合う制度を探しやすくするサービスです。
jotter.me
個人開発者のためのホスティング一体型ノートサービス
ハンドミラー
iPhone向けの手鏡アプリ
ツイプロ(twpro)
Twitterプロフィールの高速検索エンジン