1. Search for a few days to solve the Ajax polling in the user volume when the card dead browser, comet, SIGNALR and so on. Have not been satisfied with the solution, and finally the use of websocket temporary solution
2. Solution, or need to use the. NET Framework version 4.5 under IHttpHandler
1 Public voidProcessRequest (HttpContext context)2 {3 if(context. Iswebsocketrequest)4 {5 context. Acceptwebsocketrequest (Dorespond);6 }7 } 8 Private AsyncTask Dorespond (aspnetwebsocketcontext context)9 {TenSystem.Net.WebSockets.WebSocket socket =context. WebSocket; One while(true) A { -arraysegment<byte> buffer =Newarraysegment<byte> (New byte[1024x768]); -Websocketreceiveresult result =awaitsocket. Receiveasync (buffer, cancellationtoken.none); the if(socket.) state = =Websocketstate.open) - { - stringUsermessage = Encoding.UTF8.GetString (buffer. Array,0, result. Count); -Usermessage ="Test message:"+usermessage; +Buffer =Newarraysegment<byte>(Encoding.UTF8.GetBytes (usermessage)); - awaitSocket. SendAsync (buffer, Websocketmessagetype.text,true, cancellationtoken.none); + } A Else at { - Break; - } - } -}
View Code
3. Interface end, you can use the manual call, or combined with a JS timer can be implemented, the server constantly push the message over, open chrome debugging will not see there is a back-to-side request. After testing the same number of users, it is much more efficient than the previous Ajax polling. It also needs to be studied in a simpler, more scientific way to address
1 varsocket;2 3 $ (document). Ready (function () {4 if(!modernizr.websockets)5 {6Alert"This browser does not the support Web sockets");7 }8 9Socket =NewWebSocket ("ws://localhost:49464/websockethandler.ashx");Ten OneSocket.addeventlistener ("Open", Function (evt) { A$("#divHistory"). Append (' with the server.'); -},false); - theSocket.addeventlistener ("Close", Function (evt) { -$("#divHistory"). Append (''+ Evt.reason +''); -},false); - +Socket.addeventlistener ("message", Function (evt) { -$("#divHistory"). Append (''+ Evt.data +''); +},false); A atSocket.addeventlistener ("Error", Function (evt) { -Alert'Error:'+evt.message); -},false); - varRtimer; - function Loadriskwindow () { - //Rtimer = Window.setinterval ("Loadmyfundswork ()", +); in}
View Code
Apply WebSocket in ASP.