使用Mochiweb基于Comet架设类似Facebook的Web聊天系统(原创)

使用Mochiweb基于Comet架设类似Facebook的Web聊天系统(原创)
作者:余超 EMAIL:yuchao86@gmail.com

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/。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值