Solved

how to visually detect online-offline status of lab PCs

Posted on 2010-09-02
16
578 Views
Last Modified: 2012-08-14
I need a way of visually checking the internet connection of 50 PCs of my (unstable) wifi lab. I want to see at a glance (for several minutes) when they go offline/online. I discarded solutions that would need me to install something on every single PC (standalone programs, etc) so I prepared a webpage that could do the trick (as you can see, in my script I'ìm changing the backgroung color to make it easy to visually identify which PCs are offline/online).

The webpage loads a remote image. If it succeeds, than it says that the PC is online. Using setInterval I intend to keep running the function every each second.

But it doesn't work as I suppose it should: it correctly says that I'm online, but when I intentionaly put my PC offline it keep saying that it is still online. I suppose that this is because the tempImage object, for some reason, is  not recreated each time, but it is the one that was created the very first time.

Whatever solution using html/javascript/php to me would be good, what I need is a completely working solution, or the precise identification in my script of where the problem is and what the solution is. Even an online service that would allow me to visually detect my online/offline status would work for me.

<html>
<head>
	<script type="text/javascript">
	<!--
		var tempImage;

		function checkOnlineStatus() {
			document.getElementById('onlineStatus').innerHTML = 'Checking online status, please wait...';
			if( tempImage != null) {
				delete tempImage;
				alert("object deleted");
			}
			var tempImage = new Image();
			tempImage.onload = returnOnlineStatus;
			tempImage.onerror = returnOfflineStatus;
			tempImage.src = 'http://www.google.co.uk/intl/en_uk/images/logo.gif';
		}

		function returnOnlineStatus() {
			document.getElementById('onlineStatus').innerHTML = '<h1><center>Online</center></h1>';
			document.bgColor = "#008000";

		}

		function returnOfflineStatus() {
			document.getElementById('onlineStatus').innerHTML = '<h1><center>Offline</center></h1>';
			document.bgColor = "FF0000";
		}

	//-->
	</script>
</head>

<body onload="setInterval('checkOnlineStatus()', 1000);">
	<div id="onlineStatus">Checking online status, please wait...</div>
</body>
</html>

Open in new window

0
Comment
Question by:Esanteva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 3
  • +1
16 Comments
 
LVL 13

Expert Comment

by:ansudhindra
ID: 33585148
Since you are loading image from internet to check the connection status,  the image meight be cached by your browser. So you are not able to achieve your goal with this.

You can try creating an IFrame and an array of websites (http://www.google.com, etc.). And after interval of time change the source of the iFrame to load diferent sites that are mentioned in the array.
0
 

Author Comment

by:Esanteva
ID: 33585264
That wouldn't work, I need the test runnig every 10 seconds for about half an hour (180 iframes are too many for me). I know that with PHP is possible to avoid caching pages. It is the same also for images?

And if with images my online detection page won't work, is there another way using php, xmlhttprequest, ajax, etc?
0
 
LVL 3

Expert Comment

by:JtR
ID: 33585280
LOL maybe i am too stupid, but i don't get your point. your script only shows yourself if you are online or not. when you want to see if the other 50 pcs are online or not than you have 2 ways:

1. you send a ping to each of them. (probably you will not get them all because of icmp blocking.

2. you force a http request on every single of that pcs (i guess this is what you like to do?)
you have a webpage on each of that pcs running and this webpage reloads content from your web proxy so that you get a feedback of them??

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 3

Expert Comment

by:JtR
ID: 33585291
so first the question is: can you force your 50 pcs to load something from a webserver? (for this you need to keep a browser session open in the background)
0
 
LVL 3

Expert Comment

by:JtR
ID: 33585320
to your second question:

one trick to avoid caching would be if you put an ?x=.... on the end of your script.

you can do that also with images. for example you could randomize something and put it at the end of the image request:


tempImage.src = 'http://www.google.co.uk/intl/en_uk/images/logo.gif?'+randomfunction( );
0
 

Author Comment

by:Esanteva
ID: 33585507
Yes, I need to show the same webpage on all PCs of the lab (I manually open the page when I'm in front of them).

I replaced line 16 with

tempImage.src = 'http://www.google.co.uk/intl/en_uk/images/logo.gif?x='+Math.random( );

but it keep saying that I'm offline.

I verified that I get an error when I put, for example, the address

http://www.google.co.uk/intl/en_uk/images/logo.gif?x=0.86 

in my web browser.
0
 
LVL 1

Expert Comment

by:talktodhand
ID: 33585952
Hi Esanteva,


Try following code:

<html>
<head>
      <script type="text/javascript">
            
            function returnOfflineStatus() {
                  document.getElementById('onlineStatus').innerHTML = '<h1><center>Offline</center></h1>';
                  document.bgColor = "FF0000";
            }
            function reloadPage() {      
                  setTimeout("reloadPage()",5000);
                  window.location.reload();
            }
      </script>
</head>

<body onload='reloadPage();'>
      <div id="onlineStatus"><img src='http://www.google.co.uk/intl/en_uk/images/logo.gif' onerror='returnOfflineStatus();'><h1><center>Online</center></h1></div>
</body>
</html>


Hope it will help.
0
 
LVL 3

Accepted Solution

by:
JtR earned 500 total points
ID: 33586018
ah hehe, yes i see you are right i get an error too at that google pic because it probably is generated dynamically.

you see here:

http://www.heise.de/icons/ho/heise_online_logo.gif?x=123

with real pics it works.

anyway, so best for you is to do take a tracking script that throws out an image. - put a PHP-Script on your webserver  that you can call like that:

<img src="http://www.myserver.tld/mytrackimage.php?randomstring=randomstring" />

that generates that image (in simpliest case you take a transparent 1x1.gif

so your script needs to do 2 things:

1. throw out the image code
2. track the request and log it into a file or database for providing your monitor script
0
 
LVL 3

Expert Comment

by:JtR
ID: 33586077
i have written that kind of service already for a webcast system where the boss wants to see how many people are watching/listening to the webcast.

there the problem was, that the tracker script (in your case the image script) did not reload because the browser thought it is the same and it cached the output (even when i forbit it via pragma and no-cache settings inside the http header). - i needed to bring in some random codes into the output of the tracking script as well to avoid that effect. but i think in your case it will work because the browser normally resolves the cache by lookup in a key value store whichs key it builds by the request URI...
0
 
LVL 1

Expert Comment

by:talktodhand
ID: 33586105
Note:This works fine with FireFox.
0
 
LVL 3

Expert Comment

by:JtR
ID: 33586241
yes, but it is not his problem i guess... or maybe it's a part of his problem..
as far as i see he wants to detect if OTHER persons using a specific website are online or not?!

but his own status is important too when he has a monitoring page on his own computer and this monitoring page fetches the infos via ajax.. but for this also exist another possibility in HTML5 standard) "navigator.onLine"-object
0
 
LVL 3

Expert Comment

by:JtR
ID: 33586252
@talktohand - only wanted to say that in Firefox you don't need your solution as there is a specific object for it ;)
0
 
LVL 1

Expert Comment

by:talktodhand
ID: 33586396
@JtR - He has requirement that he will open html page on every pc in lab and when connection goes, "offline" message will be displayed.

His code tells me that!!! I don't know from where you got idea of monitoring  PC's for specific website !!!!!!
0
 
LVL 3

Expert Comment

by:JtR
ID: 33586485
well... was my intuition.. or maybe it was his first sentence:

I need a way of visually checking the internet connection of 50 PCs of my (unstable) wifi lab. I want to see at a glance (for several minutes) when they go offline/online.

i did not thought it to be so simple??? - why should he only be interested in showing his users if they are online or offline?? i mean your network manager tells you when you are offline... :)

but maybe you are right. when it's like this. then he is done with your solution.
0
 
LVL 3

Expert Comment

by:JtR
ID: 33586506
@talktodhand:

but i like your attitude.. you are starting to work and think in "coding" at once.. :)
maybe i am missing that spirit of an hacker at my own person.. ^^
0
 

Author Closing Comment

by:Esanteva
ID: 33587112
> with real pics it works.

Yes, that was the problem. Now my scripts works perfectly. Thanks a lot.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question