Tuesday, 11 February 2014

Firefox offline detection

In Firefox find is in offline.

In chrome , We can use  following code to detect the user in offline or online

if(navigator.onLine ){
 alert('online');
}else{
alert('offline');
}

But above code not work in firefox.For the purpose we can use dummy ajax request . Based on response time
We can say  user in online offline.

For Java application the code script as follow.

var run = function(){
  $.ajax({
    url: "/RedirectPage",
    type: "POST",
   cache: false,
   contentType:false,
              processData: false,
              timeout: 3000,
    success: function(data){
   
     if(data!='y'){
      
      alert("Your Internet Connection Lost."); 
      
       
     }
    },
   error: function(XMLHttpRequest, textStatus, errorThrown) {
           if(textStatus == 'timeout') {
              
         alert("Your Internet Connection Lost.");
           }
       }
   });
 };
 
 
 
  setInterval(run, 5000);//I will call for every 5 seconds.






In Java Code as follow.

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RedirectPage
 */
public class RedirectPage extends HttpServlet {
 private static final long serialVersionUID = 1L;

 @Override
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/plain");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("y");
  return;
 }
}

web-xml

    <servlet>
    <description></description>
    <display-name>RedirectPage</display-name>
    <servlet-name>RedirectPage</servlet-name>
    <servlet-class>RedirectPage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RedirectPage</servlet-name>
    <url-pattern>/RedirectPage</url-pattern>
  </servlet-mapping>
  <servlet>

No comments:

Post a Comment