title: How to detect client side internet disconnection using Node.js
body: <p>I am using node.js for game server . </p>
  
  <p>Here is my server script</p>
  
  <pre><code> var net = require('net');
   var http = require('http');
  
  var host =  '192.168.1.77';
  var portNum = 12345;//
  
  
    function policy() 
  {
      var xml = '&lt;?xml version="1.0"?&gt;\n&lt;!DOCTYPE cross-domain-policy SYSTEM' +
              '"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"&gt;\n&lt;cross-domain-policy&gt;\n';
  
      xml += '&lt;allow-access-from domain="*" to-ports="*"/&gt;\n';
      xml += '&lt;/cross-domain-policy&gt;\n\0' + "\0";
      return xml;
  }
  
  
  
  var server = net.createServer(function (stream) 
  {
  stream.setEncoding('utf8');
  
  stream.on('data', function (data) { 
  
      if (data == '&lt;policy-file-request/&gt;\0') 
      {
          var x = policy();
          stream.write(x);
          var server_date = new Date();
          var serv_sec = server_date.getSeconds();
          return;
      }
  
      var comm = JSON.parse(data); 
      if (comm.action == "Join_Request"  &amp;&amp; comm.gameId =="game1") // join request getting from client
      {
          var reply0 = new Object();
          reply0.message = "WaitRoom";
          stream.write(JSON.stringify(reply0) + "\0");   
       }
   });
  
  stream.on('disconnect', function() 
  {
      console.log("disconnect");
   });
  stream.on('close', function ()  
  {
  console.log("Close");
  }); 
  
   //stream.setNoDelay(true);
  //stream.setKeepAlive(true, 200);
  //stream.setTimeout(10, function(){
  //  console.log('timeout');
  //});
   stream.on('connect', function() {
   console.log('check 2', stream.connected);
   }   );
    stream.on('error', function () { 
    console.log("Error");
    }); // close function 
  
    });  // ===== create server end
    server.listen(portNum,host,{'heartbeat interval':1,  'heartbeat timeout' : 2}  );
  </code></pre>
  
  <p>===============================================================================================</p>
  
  <p>Client side script</p>
  
  <pre><code>    using UnityEngine;
      using System.Collections;
      using System;
      using System.Xml;
      using System.Linq;
      using System.Threading;
      using Boomlagoon.JSON;
      using JsonFx.Json;
      using System.Net.Sockets;
  
                        try 
                          {
  
                              tcpClient.Connect (host,portNum);
                              serverStream    =  tcpClient.GetStream ();
                              serverStream.Flush ();
  
                              ThreadPool.QueueUserWorkItem( new WaitCallback( ReceiveFromServer ) );
  
  
  
                              isConnectionActive = true;
                              isReadyToJoinRoom = true;
                              connected = 1;
                              disconnected =0;
                              c_global.isClientConnectOn = false;
                              c_global.isClientDisconnectOn = false;
  
                          }
                          catch( ArgumentException l_exception )
                          {
                              c_global.isClientDisconnectOn = true;
                              isConnectionActive = false;
                          }
  
                          catch( SocketException l_exception )
                          {
                              c_global.isClientDisconnectOn = true;
                              isConnectionActive = false;
                          }
  
                          catch(Exception e)
                          {
                              c_global.isClientDisconnectOn = true;
                              isConnectionActive = false;
                              connected = 0;
                              disconnected =1;
  
                          }
  
  
                 public void ReceiveFromServer(object stateInfo) // Please call this function once
                  {
                      print (" $$$$$$$$$$$ ReceiveFromServer  and isConnectionActive"+isConnectionActive);
  
                      while(isConnectionActive) // receive message continuously 
                      {
                          try
                          {
                              byte [] inStream = new byte[tcpClient.ReceiveBufferSize];
  
                              serverStream.Read (inStream,0,(int)tcpClient.ReceiveBufferSize);
                              string returnData = System.Text.Encoding.UTF8.GetString(inStream);
  
                              print ("*^^^^^^^^^^* returnData"+returnData);
                              Decide_Action(returnData);
  
  
                          }
                          catch (Exception e)
                          {
                              c_global.isClientDisconnectOn = true;
                          }
  
  
                      }
  
                  }
  
  
  
             public void Decide_Action(string returnData)
             {
             try
             {
  
             var r_Msg = JSONObject.Parse(returnData); // recieved message r_Msg
  
             string msg = r_Msg.GetString("message") ;
  
             print ("Message = "+msg);
  
             switch(msg ) // check the action to do
             {
             case "Players_List":
             break;
             }
  
             }
  
             catch (Exception e)
             {
                              c_global.isClientDisconnectOn = true;
             }
  
  
             }
  </code></pre>
  
  <p>When game is close or quit from game , client disconnection detect  by server . That is "close" function and "error" function is calling that time. </p>
  
  <p>But when internet disconnections from client system or device , that time "close" function or "error" function is not calling.</p>
  
  <p>How to detect this kind of client disconnection</p>
  
  <p>Client Net disconnection is not detecting by Node.js server . </p>
  
  <p>Please help me , if anyone know the solution .</p>