[Instant message] Use asp.net + jquery to implement the comet instant message mechanism

Source: Internet
Author: User

:

 

The client connection status and message status are managed through background threads.

Webconfig Configuration

 

 

<Deleetask>
<Add key = "IntervalTime" value = "10000"/> Interval
<Add key = "HasDB" value = "N"> </add> whether to query database system information
<Add key = "ConnectInterval" value = "4000"> </add> connection Interval

</AppSettings>

 

 

 


<HttpHandlers>

<Add verb = "POST" path = "ChatChannelHandler. ashx" type = "MDS. Comet. ChatChannelHandler, Comet"/> send messages
<Add verb = "*" path = "ChatHandler. ashx" type = "MDS. Comet. ChatHandler, Comet" validate = "false"/> AJAX round robin
</HttpHandlers>

 

1.asp.net

When the customer segment is connected for the first time

ThreadPool. RegisterWaitForSingleObject (client. EventWaitHandle, new System. Threading. WaitOrTimerCallback (WaitOrTimerCallback), client, ConnectInterval, true) adds a function that can be periodically executed to the thread pool,

Set

Public AutoResetEvent EventWaitHandle = new AutoResetEvent (false );

Client. EventWaitHandle

To send messages to the client immediately, and use this function to achieve persistent connections between the client and ensure the client's sustainability.

 

 

 

2. AJAX round robin

 

 

Ajax

Function SetParameters (p, pSplit)
{
Var option = {length: '20140901', title: '', help: 'none '};
Properties = p. split (pSplit );
For (I = 0; I <properties. length; I ++)
{
Namevalue = properties [I]. split (':');
If (namevalue. length> 1)
{
Execute = "option." + namevalue [0] + '= \ ''+ namevalue [1] + '\';';
Eval (execute );
}
}
Return option;
}

Function CometClient (handler, publicToken, privateToken, onmessage, ontimeout, oncancelled, onerror)
{
This. lastMessageId = 1000;
This. publicToken = publicToken;
This. privateToken = privateToken;
This. handler = handler;
This. onmessage = onmessage;
This. ontimeout = ontimeout;
This. oncancelled = oncancelled;
This. onerror = onerror;
This. running = true;
}

CometClient. prototype. stopWaiting = function ()
{
This. running = false;
}

CometClient. prototype. startWaiting = function ()
{
This. running = true;
This. waitForMessages ();
}

CometClient. prototype. waitForMessages = function ()
{
Var me = this;

$. Ajax (
{
Url: this. handler,
Type: "POST ",
Data: {privateToken: this. privateToken, lastMessageId: this. lastMessageId },
Success: function (response)
{
Var messages = null;
If (response! = Null & response! = "")
{
 
Messages = SetParameters (response ,";");
}
If (messages! = Null & messages. length> 0)
{
Var keepWaiting = me. running;
For (var I = 0; I <1; I ++)
{
If (me. lastMessageId <messages. MessageId)
Me. lastMessageId = messages. MessageId;

Switch (messages. Name. toLowerCase ())
{
Case "timedout ":
Me. ontimeout ();
Break;
Case "canceled ":
KeepWaiting = false;
Me. oncancelled ();
Break;
Case "message ":
Me. onmessage (messages );
Break;
}
}
If (keepWaiting)
Me. waitForMessages ();
}
},
Error: function ()
{
Me. onerror ();
}
});
}

 

Download DEMO code

 

 

Download the code COMET. DLL that implements persistent connection.

 

 

Another Implementation Method

FluorineFx + Flex is implemented as follows.

Http://ftp-woody.d134.6266668.cn/chat2.aspx

 

Related Article

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.