Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Force client window refresh

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
NL
The question only seems simple...

I am trying to create an online game, using php/mysql and javascript. Most of the functions I am going to need won't be a problem. One, crucial however, I'm not sure about.

A game will allow to play four users (clients) to play against each other. The actions of one may lead to a refresh of the other clients window (or parts of it). Is this possible with javascript/php or am I aiming in the wrong direction?

If it's possible, tell me how. If it's not, where should I be looking?

Don't eat yellow snow!
 
I must add that I already think php isn't the right code for the job. But I like another man's or woman's comment.

Don't eat yellow snow!
 
to do anything like that you will need to be looking at Java servlets I would think.

try forum197

Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
I too think that JAVA or Flash will be the sollution here.
Javascript is meerly client-based, while PHP is server-side.
You can make PHP parse javascript, but php will only run when you refresh the page.

There is only one way to do this with javascript and php/mysql I think:

A javascript which has an openWindow() code, with a page that has meta-refresh. That page should have NO GRAPHICS at all! (for fast loading). It should check for updates in the SQL, if any updates, reload the main window.

I personally dont like javascript, if you know java, that is a better way to go.
Flash is also possible to connect to mysql or just use with php.. I guess flatfiles are no good for games.. (slow and crappy to administrate).

good luck :)
 
Since PHP is executed on the server there is no way to change anything in the page after it is sent to the browser (as far as PHP is concerned).

SO, you will need client side scripting.

You can combine the two technologies by binding them together with an HTML technique: <iframe>

You have an iframe in the page that constantly reloads (on a specific schedule) and updates values in the loaded page using e.g. JavaScript. That would work fine and not require any applet technology.
 
You can do this with no Javascript, and with no PHP, and no Java! (In my view, Javascript is "BAD" - try to avoid using it). What you can do is send a header that makes the page expire after a number of seconds, like this:
Code:
<HTML>
<META HTTP-EQUIV="refresh" content="60;url=theurltothispage.html">
<HEAD>
... the rest of your page...

(by the way, The 60 is in seconds.)

I hope this helps.
 
paulharding
Ever heard about the wonderful setting in IE:
Disable META refresh?

General:
JavaScript is fine where it belongs and where it's presence can be mandated. Players of your game will have to fulfill the minimum requirements to play. I think saying JavaScript is "BAD" is a bit exaggerated. Let's not return to the Web without image rollovers.

 
Yes, it is a bit exaggerated, I concede.

META refresh can of course be disabled, but you then go on to say that the game environment can be mandated... Javascript can be disabled too, as you know.

What I was trying to achieve was to show how some things can be done without any programming.
 
Of course, I see your point and acknowledge it. However, a game that would change every 60 seconds in a high-speed world seems rather lame, unless it is something genial like chess...

Your contributions to the forum are genuinely appreciated.
 
Yes, I was thinking of puzzle games and the like, since PHP wouldn't be too suited to a fast game, as Quasibob rightly says up at the top.

I use the "META" technique to refresh a PHP/MySQL groupware diary, where one user adds a diary note and so we need to change the view of everyone else. In this context, 60 is fine.

I appreciate your feedback.
 
How can you say that he can make a game without a programming language?
He needs server-side programming to gather data from the sql database, as well as store user input. (unless it's client/server based, which he did not express that he wanted)

No matter what the game is, he needs a "black box", which the user will never see. This black box will maintain data-stream. You cannot make a "html" game, without a language like java, javascript, php, asp or similar. html in it self, is just a way to structure content.

How he is to refresh the page, is another matter. I too said that php can not do client-side! But php can also provoke a page-refresh! You do not need html meta-refresh to do this, there is a header() function, which is WAY better than meta-refresh.
meta-refresh with low timers actually gets penalized by google and other search-engines!

Also the iframe has support in less browsers than javascript! So iframe is not to favour over javascript.

Seeing as we know nothing about his game, there is no "one fix all" sollution here!
But nomatter how he wants his game to be, he needs some programming language to control his users, his data and his graphic!

* If his game is tick-based, he needs something to generate ticks.
* If his game is turn-based, he needs something to refresh page when the other player has used his turn or the other player has left the game.
* If the game is "realtime", he needs something that updates the page, whenever the other player has done something.

I think that for none of those game-types, a meta-refresh would be o.k. It's not good to refresh the page all the time.

PHP, while beeing server side, will only run on refresh, so php can not "scan" for news, unless page has been refreshed.

If you where to use an Iframe to check for updates, for gods sake use header() with permanently moved (301), or you will not get high stats in google for sure! (which again will make your game less attractive, since it will gain less visits).

You could also use javascript openWindow, but I would not recommend that either.

I cant actually say that I would recommend anything that has to "pop up" or refresh parts of, or your entire page.
I think that flash or java would be the best way to go, maybe in cooperation with php/sql and some xhtml.

backend:
php/mysql and maybe perl (if ticks needs to be generated).
frontend: java-applet or flash, presented via xhtml 1.0

If you have crontab, you can run scripts, like let's say perl. perl can then generate ticks, if your game is tick based.. (pimpwar, planetarion, and more, where tick-based).
If you game is turn-based, you need something which is not html.. eg. java or flash. Something which can change content without refresh. The datastream should not refresh the page, even if the game is chess!

for a tick-based game, I guess refresh is o.k. but then you dont actually have to refresh the page, as the user will go back and forth all the time, between pages, which then refreshes his stats, including his ticks.¨

If you have any examples of HTML games (multiplayer, over internet), please do show me. I have never seen any html games, which do not contain any java, javscript, php, mysql, asp, or any other programming language.

I dont mean to be rude, please come back with more suggestions and/or arguments.
 
Hi DaButcher,

I never said you could do the game without programming - I was just showing how a refresh could be done without programming.

Kind regards.

 
Maybe I read your statement "What I was trying to achieve was to show how some things can be done without any programming." somewhat out of context.

however, no matter what way he chooses to refresh the page, it's not a good idea, unless it's a tick-based game.

I would say though, that if it was a tick-based game, I would rather make php parse some meta-refresh, use header(), or whatever. I would not use a flat-file for refreshing a page, as the refreshing page should have some logick to check for updates in the game-action-list.

a tick-game, would maybe require refresh, but in my mind, a tick game could also do fine without.
 
There's some other choice(s) here as well.

You could have javascript set to check at regular intervals (like once a second or something), using SetInterval(), for an updated "message" from the server, and if it gets the "refresh" message, it does a window.reload() refresh command.

... you say, how does javascript do that... well i'm glad you asked...

1. (probably best method) a hidden iframe/layer, where you can send URL requests through to a PHP script, and then "parse" the response output that is returned to inside that iframe/layer.

2. have javascript on it's timeout instantiate a new Image object, and set it's source to the location of a PHP script, and then the response is like yes or no depending on if an event like a refresh should be initiated. The way javascript knows the "yes" or "no" is to check to see if the image loaded successfully (and then in your PHP simply fail to send back an image stream correctly if you want to indicate a "yes", by throwing a "404 not found" header back)... this might look something like:

Code:
var img = new Image();
img.onerror=fnRefresh;
img.src = "[URL unfurl="true"]http://www.myurl.com/myscript.php?gmsg=123";[/URL]

function fnRefresh() {
  window.reload();
}

FYI:

a. this method is asynchronus, meaning you can't expect for an immediate yes/no answer from the setting of .src, to decide what to do next... (in other words, the img.src= command does NOT wait to see a response from the server before going on to the next command). you have to have a call-back method as above, to handle the "error" a split second later.

b. it looks like IE might sorta "cache" a call to an image that returns a "valid" response (in other words, no error). Subsequent calls to the image with the exact same URL will not fire the error, even if the image is gone, or your php script returns an invalid image stream (404 error, etc). To get around this, just make sure the URL that you use to load the image src has a random number on the end of it, like:

img.src = "
that r= parameter can be ignored by your PHP script, you are simply trying to make sure the "URL" of your image request is ALWAYS different to defeat the cache'ing done by the browser.

3. This option is IE only, but is very reliable, and is synchronous. it uses the built in HTTPClient (activeX) plugin that IE has by default, which allows your code to execute a sub-request to a URL, and you can then parse it's results, much like if there had been a hidden iframe/layer making the request.
 
In response to shadedecho..

I dont understand why you would want to use javascript to do this, not in this way..

Why would you want to use an image?

Then you might as well use an openwindow, iframe, or just open a new window! (an independent page, which refreshes itself).

Why is this better, you say?
- Then you can have a server-side refresh! (I think you cant rely on the client for validation, etc. since then the client may manipulate this for his/her advantage).

serverside refresh: (header:location)
meta-refresh can also be used.

Dont mis-understand me, I would not do any of the listed things, unless based on something that streams data. java applet, flash, or whatever.
 
DaButcher--

I won't argue that there are several other client-side "applet" like technologies which would provide a much more reliable way to force screen-updating for this "game".

However, the original question said :

Is this possible with javascript/php or am I aiming in the wrong direction?

If it's possible, tell me how. If it's not, where should I be looking?

It is possible with javascript and PHP, and so that is how I responded.

Clearly there are other methods which could provide similar or better results. But then again, this IS a PHP forum... the fact that we branched out into a deep discussion of javascript was an exception because the question required addressing that issue at least to some extent.

If we were in the Flash forum, I would have given a much different answer. :)
 
shadedecho:
I'm just a bit afraid that someone will give him a sollution that will not work as he wants it to.
that's just why I come with arguments that I guess most programmers know.

It's not fun for him, if he uses 50++ hrs. And he cant use his project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top