Jump to content

fsockopen and ajax help


Furt

Recommended Posts

Im using fsock to ping a server to see if its online or offline, my script handle more than one server and it is hurting load time of page so with that said id like to have ajax load the server ping script so that the rest of the page doesnt get dragged down with it

 

Im still very new to ajax and wonder if i can get pointers on how to use ajax to load this script.

This is the full function im using fsock in.

 

	function playeronline($id,$name, $ip, $port, $mysql, $sqlun, $sqlpass, $chardb, $maxplr) {
	connectremotes($mysql, $sqlun, $sqlpass, $chardb);
	$queryup = "SELECT  count(online) AS result_count FROM characters WHERE online = '1' LIMIT 1";
	$result2 = mysql_fetch_assoc(mysql_query($queryup));

	$countnum = $result2['result_count'];
	if($countnum <= "09") {
		$cntfin = str_replace("0", "", $result2['result_count']);
	} else if($countnum == "00") {
		$cntfin = "0";
	} else if(empty($countnum)) {
		$cntfin = "0";
	} else {
		$cntfin = $result2['result_count'];
	}
	$total = floor(($maxplr - $cntfin)/10);

	if($cntfin == "0") {
		$plr = "0";
	}else if(empty($cntfin)) {
		$plr = "0";
	} else {
		$plr = $cntfin;
	}

	if (! $sock = @fsockopen($ip, $port, $num, $error, 5))  {
		$fontcolor = "#FF0000";
		$link = '<img src="down.php?name='.$name.'" border=0>';
	} else {
		$fontcolor = "#00FF00";
		$link = '<a href=?pageid=status&id='.$id.' style="text-decoration:none"><img src="up.php?name='.$name.'" border=0></a>';
	}
	$final = '
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
			    <td height="10px" align=center>'.$link.'</td>
			</tr>
			<tr>
				<td><table width="100px" align="center" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td height="10px" width="'.$$cntfin.'" style="background-image:url(includes/images/status/bar2.png)"></td>
				<td height="10px" width="'.$total.'" style="background-image:url(includes/images/status/bar.png)"></td>
			</tr>
		</table></td>
			</tr>
			<tr>
				<td height="10px" align=center><center> <font style="font-size:9px; font-family:Tahoma, Geneva, sans-serif">('.$plr.' / '.$maxplr.' ) Players Online</font></center><br /></td>
			</tr>  
		</table>
	';
	return $final;
	mysql_close();
}

 

and the actual page that calls it.

<?php
if( ! defined( 'FCMS' ) ) {
die( "Hacking attempt!" );
}

$sql = new sql();
$sql->wconnect();;

$realmquery = "SELECT * FROM realmlist";
$realmresult = mysql_query ($realmquery);
while ($rrow = mysql_fetch_assoc($realmresult)) {
	$id = $rrow['id'];
	$name = $rrow['name'];
	$ip = $rrow['realmip'];
	$port = $rrow['realmport'];
	$mysql = $rrow['mysqlip'];
	$sqlun = $rrow['mysqluser'];
	$sqlpass = $rrow['mysqlpass'];
	$chardb = $rrow['characterdb'];
	$maxplr = $rrow['maxplr'];

echo playeronline($id,$name, $ip, $port, $mysql, $sqlun, $sqlpass, $chardb, $maxplr);
}

?>

 

Any advise would be helpful.

Link to comment
Share on other sites

this answer is PROBABLY a little off topic here, HOWEVER, due to browser security in some areas and some browsers you really are limited with javascript, from your user to your server thats 1 request your server handles then from your server to the server getting pinged.. its really pointless to have your server handling all these requests per visit,

 

What you SHOULD do is set up a 1px by 1px flash app and set up an externalInterface callback so that javascript can tell the flash file to do the socket opening and if it opens with no errors, thats a good thing, you need a crossdomain.xml file on their site ONLY IF you're planning to receive data, sending data to them is perfectly fine, however to accomplish what I'm saying you need to do neither of the two.

 

Just connect, no errors, GOOD, errors? BAD. close connection and wait for more instructions.

 

This way you're not wasting bandwidth, you're not overusing your server.. The user is making all of the requests.. and for them it'd be faster than waiting for a server that is handling 4,000 requests to serve them, they're using their own network connection to test it.

 

and with flash you can send a javascript call aswell to display whether its active or not..

 

its really nifty :) I could write one up for you but then it wouldn't be considered helping :)

Link to comment
Share on other sites

99.9% of the time the fsock connect would be localhost and really only checks to see if a certain port is in use if that makes a difference.

 

if tried tinkering a bit since i made this post but i havent got it to work properly.

 

statusloader.php

<script type="text/javascript">
function ajaxFunction()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.getElementById("sloader").innerHTML=xmlHttp.responseText;
        }
      }
    xmlHttp.open("GET","modules/serverstat/smallstats.php",true);
    xmlHttp.send(null);
  }
</script>
<div id="sloader"> 
  <br/>
  <br/>
<center>Loading...<br><img src="includes/images/status/loading.gif" /></center>
  <br/>
  <br/>
</div>

 

Ive tried starting it with this but it just stays at loading.

 

It was an example i found and just changed a few things.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.