简体   繁体   中英

ajax - Best way to keep a long polling connection open

I am developing a simple chat application for learning purposes. I am using long-polling to keep my chat updated. Now this is what I am doing-:

  1. Client makes a request to server.
  2. If server has update then it sends it. Otherwise step 3.
  3. Server lets the request hang until update is available.
  4. If request times out server sends back "Keep-Alive" and user makes a new request (step 1).

Is this the best way to do it ? Is there anything more optimized than this ? Should I consider HTTP Streaming to HTTP Long Polling since it reduces overhead ?

I am using Java Servlets to do this but this more of an AJAX question, so thats why I didn't add any code. I will if that's required.

What you have suggested would certainly work. Another options is WebSockets, though it is relatively new technology and it is not supported in older browsers.

Web Sockets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM