:
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