Use Mochiweb to build a Facebook-like Web Chat System Based On Comet (original)

Source: Internet
Author: User

Use Mochiweb to build a Facebook-like Web Chat System Based On Comet (original)
Author: Yu Chao EMAIL: yuchao86@gmail.com

Facebook's web chat system is implemented using Mochiweb Based On Comet technology. Currently, HTTP is required by clients to send information to servers, and the server cannot push information to clients. For example:
1. When the email server receives a new email, it reports it to the user in time. If the email server is online
2. When the Web chat server receives new information, it sends the information to the corresponding online user in a timely manner.
3. When chatting, you are prompted that the other party is entering the information.
When implementing these applications, you will have trouble.
Next we will implement a Mini Web chat system.

The following describes how to configure Mochiweb Based on Erlang:

1. Download and compile MochiWeb and obtain the mochiweb source code from SVN.
[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
The next step is to compile the Mochiweb source code. If you do not think it is good or want to improve it, you can go to the src directory to modify the file.
[Yuchao @ yuchao-Latitude-E5410 Erlang] $ CD mochiweb
[Yuchao @ yuchao-Latitude-E5410 mochiweb] $ make
(Cd src; make all)
/Home/yuchao/Erlang/mochiweb/src
Make [1]: Entering the directory '/home/yuchao/Erlang/mochiweb/src'
ERlC-w-I ../include + debug_info-o ../Ebin mochifmt. erl
Some compilation output is omitted in the middle...
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 contains invalid mochihex mochijson mochilists mochilogfile2 mochinum mochitemp mochiutf8 contains invalid mochiweb_cookies
Mochiweb_cover mochiweb_echo mochiweb mochiweb_headers mochiweb_html mochiweb_http mochiweb_io mochiweb_mime mochiweb_multipart mochiweb_request mochiweb_response when mochiweb_socket already contains reloader"
Make [1]: leaving the directory '/home/yuchao/Erlang/mochiweb/src'
Set $ mochiweb Environment Variables
[Yuchao @ yuchao-Latitude-E5410 mochiweb] $ mochiweb = 'pwd'
2. Download the source code of 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
Set the soft connection in the following step so that mochiweb-src points to $ MOCHIWEB. If you don't need it, you can also put the MOCHIWEB source code below mochiweb-src.

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

[Yuchao @ yuchao-Latitude-E5410 deps] $ ls-al
Total 8
Drwxr-xr-x 2 yuchao 4096.
Drwxr-xr-x 9 yuchao 4096 ..
Lrwxrwxrwx 1 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]: Entering the directory '/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]: leaving the directory '/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

In this step, a webchat server is started at http: // localhost: 8000. You can connect to the server from multiple machines (Multi-browser window) to chat.

Simply put, the client sends a request (establish a connection) to the server during initialization and the request is blocked until an event occurs, after obtaining and processing the event, the client re-establishes a new connection. Because requests do not return immediately when no event occurs, this greatly reduces network traffic, but does not reduce the number of connections on the server. See http://cometdaily.com /.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.