Results 1 to 4 of 4

Thread: Server-side interrupt-driven client refresh.

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Server-side interrupt-driven client refresh.

    Scenario
    I'm developing a 2 player turnbased PHP game.

    Each turn can take 1 second to 10 minutes.
    When player1 has made his move, then the webserver will store his move in a database. After that the 2nd player (who visits the same website but from another computer, and another useraccount) has to make his turn. Both players leave the playboard open in their webbrowser while they are playing.

    The problem is: how does the browser of player 2 know when to refresh.
    Usually in automation there are 2 possible solutions for this problem.
    1) Polling
    2) a Trigger/Interrupt

    Solution 1: (the polling) is easy, I can indeed write a little ajax script that calls a php page in the background, when the php page finally returns a certain text "REFRESH-OK" then I call a javascript refresh function. So, that's indeed an easy solution.

    Interrupts
    But is there also something possible like interrupts?
    I'm not sure but maybe this is what the HTTP-keep-alive is made for?

    I think the advantages of an interrupt system are obvious.
    - less data traffic
    - realtime response
    - less dependent of javascript

    Thank you in advance
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Server-side interrupt-driven client refresh.

    PHP does not continually execute; the webserver tells PHP to parse a file, and once PHP is finished with that it sends it back to the webserver, which then sends that to the client. if you want something continually polling the server for new information, you're going to have to use javascript. no server-side scripting language (as far as I know) is capable of true "real-time response."

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Server-side interrupt-driven client refresh.

    I'm not sure but maybe this is what the HTTP-keep-alive is made for?
    Nope:
    In the original implementation of HTTP, each request created a new socket connection to the server, sent the request, then read from that connection to get the response. ... [With Keep-Alive] the connection is NOT dropped, but is instead kept open. When the client sends another request, it uses the same connection.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Server-side interrupt-driven client refresh.

    Because the server cannot open a HTTP connection to the client you have to use a kludge method which you alluded to under "Solution 1". Once the page has loaded, use a XMLHttpRequest object to open a connection to a server-side script which returns once there is new data.

    You might find that you need to flush the connection every 20-30 seconds even if there is no data. This is because the client will eventually time-out and the server will also kill the script once it reaches the maximum execution time.

    There is no real disadvantage to this method other than being slightly inelegant. The only resources consumed are on the server side during the process of polling for new data (perhaps executing a simple SQL query once every second or two). There is no actual data transferred between client and server until either a response is returned or the connection drops out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width