使用Mochiweb基於Comet架設類似Facebook的Web聊天系統(原創)

使用Mochiweb基於Comet架設類似Facebook的Web聊天系統(原創)
作者:餘超 EMAIL:[email protected]

Facebook公司的網頁聊天系統就是使用這個Mochiweb基於Comet技術實現的,目前HTTP協議都是客戶端向服務器要,服務器沒有辦法推送信息給客戶端,例如:
1.當郵件服務器收到新的郵件時,及時報告給用戶,如果他在線的話
2.當Web聊天服務器收到新的信息時,及時發送信息到相應的在線用戶
3.聊天時提示你對方正在輸入等等
在實現這些應用的時候就會遇到麻煩。
下面我們將實現一個Mini的Web聊天系統.

下面開始基於Erlang的Mochiweb的配置:

1.下載並編譯MochiWeb,從SVN取得mochiweb源碼
[yuchao@yuchao-Latitude-E5410 erlang]$pwd
/home/yuchao/erlang
[yuchao@yuchao-Latitude-E5410 erlang]$svn co http://mochiweb.googlecode.com/svn/trunk mochiweb
[yuchao@yuchao-Latitude-E5410 erlang]$ls
agent_server.beam  agent_server.erl  code  dets_test.erl  ets_test.erl  jaerlang-code.tgz  memtest.c  mochiweb
接下來是編譯Mochiweb源代碼,如果你覺得不好,或者需要改進的地方,你可以到src目錄下去修改文件。
[yuchao@yuchao-Latitude-E5410 erlang]$cd mochiweb
[yuchao@yuchao-Latitude-E5410 mochiweb]$make
(cd src;make all)
/home/yuchao/erlang/mochiweb/src
make[1]: 正在進入目錄 `/home/yuchao/erlang/mochiweb/src'
erlc -W -I ../include  +debug_info -o ../ebin mochifmt.erl
... ...中間省略部分編譯輸出
erlc -W -I ../include  +debug_info -o ../ebin mochiweb_util.erl
erlc -W -I ../include  +debug_info -o ../ebin reloader.erl
../support/make_app.escript mochiweb.app.src ../ebin/mochiweb.app "" "mochifmt mochifmt_records mochifmt_std mochiglobal mochihex mochijson2 mochijson mochilists mochilogfile2 mochinum mochitemp mochiutf8 mochiweb_acceptor mochiweb_app mochiweb_charref mochiweb_cookies mochiweb_cover mochiweb_echo mochiweb mochiweb_headers mochiweb_html mochiweb_http mochiweb_io mochiweb_mime mochiweb_multipart mochiweb_request mochiweb_response mochiweb_skel mochiweb_socket mochiweb_socket_server mochiweb_sup mochiweb_util reloader"
make[1]:正在離開目錄 `/home/yuchao/erlang/mochiweb/src'
設置$MOCHIWEB環境變量
[yuchao@yuchao-Latitude-E5410 mochiweb]$MOCHIWEB=`pwd`
2.下載Minimal Web Chat的源代碼
[yuchao@yuchao-Latitude-E5410 mochiweb]$wget http://yoan.dosimple.ch/blog/2008/05/15/chat.tgz
[yuchao@yuchao-Latitude-E5410 mochiweb]$tar zxvf chat.tgz
[yuchao@yuchao-Latitude-E5410 mochiweb]$ls
chat  chat.tgz  deps  doc  ebin  examples  include  LICENSE  Makefile  priv  README  scripts  src  support  test
[yuchao@yuchao-Latitude-E5410 mochiweb]$cd chat/deps
如下這一步設置軟連接,使得mochiweb-src指向$MOCHIWEB,如果不需要,你也可以吧MOCHIWEB的源代碼放到mochiweb-src下面

[yuchao@yuchao-Latitude-E5410 mochiweb]$ln -s -f $MOCHIWEB mochiweb-src

[yuchao@yuchao-Latitude-E5410 deps]$ls -al
總計 8
drwxr-xr-x 2 yuchao yuchao 4096 2011-09-05 17:24 .
drwxr-xr-x 9 yuchao yuchao 4096 2008-05-14 18:37 ..
lrwxrwxrwx 1 yuchao yuchao   28 2011-09-05 17:24 mochiweb-src -> /home/yuchao/erlang/mochiweb

[yuchao@yuchao-Latitude-E5410 mochiweb]$cd ..
[yuchao@yuchao-Latitude-E5410 chat]$make
(cd src;make)
/home/yuchao/erlang/mochiweb/chat/src
make[1]: 正在進入目錄 `/home/yuchao/erlang/mochiweb/chat/src'
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin chat_app.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin chat_deps.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin chat.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin chat_sup.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin chat_web.erl
erl -noshell -run edoc file chat_app.erl -run init stop
mv *.html ../doc
erl -noshell -run edoc file chat_deps.erl -run init stop
mv *.html ../doc
erl -noshell -run edoc file chat.erl -run init stop
mv *.html ../doc
erl -noshell -run edoc file chat_sup.erl -run init stop
mv *.html ../doc
erl -noshell -run edoc file chat_web.erl -run init stop
mv *.html ../doc
cp chat.app ../ebin/chat.app
make[1]:正在離開目錄 `/home/yuchao/erlang/mochiweb/chat/src'
[yuchao@yuchao-Latitude-E5410 mochiweb]$./start.sh
... ... 
=SUPERVISOR REPORT==== 5-Sep-2011::17:08:58 ===
     Supervisor: {local,chat_sup}
     Context:    start_error
     Reason:     eaddrinuse
     Offender:   [{pid,undefined},
                  {name,chat_web},
                  {mfa,
                      {chat_web,start,
                          [[{ip,"0.0.0.0"},
                            {port,8000},
                            {docroot,
                                "/home/yuchao/erlang/mochiweb/chat/priv/www"}]]}},
                  {restart_type,permanent},
                  {shutdown,5000},
                  {child_type,worker}]

=INFO REPORT==== 5-Sep-2011::17:08:58 ===
    application: chat
    exited: {shutdown,{chat_app,start,[normal,[]]}}
    type: temporary

到這一步,在http://localhost:8000/就啓動了一個 WebChat Server。你可以從多臺機器(多瀏覽器窗口)連接該Server進行聊天。


簡單說來,就是客戶端在初始化的時候向服務器端發送一個請求(建立連接),該請求到服務器那裏後被阻塞,直到有事件發生後才返回,客戶端在獲得事件並處理後,再重新建立一個新連接,如此反覆。由於請求在沒有事件的時候並不馬上返回,這就大大減少了網絡流量,但是服務端的連接數並沒有減少。參閱 http://cometdaily.com/。
發佈了46 篇原創文章 · 獲贊 5 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章