I am using node.js for game server .
Here is my server script
var net = require('net');
var http = require('http');
var host = '192.168.1.77';
var portNum = 12345;//
function policy()
{
var xml = '<?xml version="1.0"?>\n<!DOCTYPE cross-domain-policy SYSTEM' +
'"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">\n<cross-domain-policy>\n';
xml += '<allow-access-from domain="*" to-ports="*"/>\n';
xml += '</cross-domain-policy>\n\0' + "\0";
return xml;
}
var server = net.createServer(function (stream)
{
stream.setEncoding('utf8');
stream.on('data', function (data) {
if (data == '<policy-file-request/>\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" && 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} );
===============================================================================================
Client side script
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;
}
}
When game is close or quit from game , client disconnection detect by server . That is "close" function and "error" function is calling that time.
But when internet disconnections from client system or device , that time "close" function or "error" function is not calling.
How to detect this kind of client disconnection
Client Net disconnection is not detecting by Node.js server .
Please help me , if anyone know the solution .
Best way is to probably handle the 'timeout' event, which is fired when there's too much inactivity. You can set how long you want that to be as well, though there's a reasonable default.
http://nodejs.org/api/net.html#net_event_timeout