Apply WebSocket in ASP.

Source: Internet
Author: User

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.

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.