I have three computers; Server
, Client
and Viewer
. I am in control of the server and the viewer.
- The user on the
Client
connects to theServer
and is presented with a webpage. - Through a php script the user uploads an image.
- The image is imbedded in some html.
- The
Viewer
is a computer totally without user interaction - there is no keyboard. TheViewer
is always at all time running a web browser, displaying the picture page.
My problem now is that even though the picture changes on the server disk, the webpage is not updated. How do I refresh the web browser on the viewer, or part of the webpage?
I know html, css, javascript, php and ajax, but apparently not well enough.
You can use AJAX requests to help. For example, what you are doing is polling the server for the image every five seconds. Instead you could poll the server for a new image id and use that id instead of the random number for the source of the image. In this case the src attribute will only change/reload when there is a new image.
An alternative is to get a notification from the server when the image changes via a web socket.
I have the exact same application. Just use
WebSockets
.You can start a
websocket connection
and the server will inform theViewer
whenever it gets an update. You can send the updated image through websocket, fully asynchronous without disturbing the display or user interaction.If you use timers, you will not get quick updates or you will keep refreshing page without use.
Details:
Will need a Websocket server like
pywebsocket
orphpwebsocket
.Client:
Will need HTML5 websocket support, all of the current browsers support it.
Need to register for a message when an image update happens. It is like registering a callback.
Example:
Whenever the server sends an update, the function mapped to
wSocket.onmessage
will be called, and you can do whatever you need to do with it.Server:
Will listen for a connection from client (can be made to support multiple clients). Once a connection is made and the message
"sendImageUpdates"
is received, the server will wait for any updates in image. When a new image is uploaded, server will create a new message and send to client.Pros
The easiest is to use AJAX pooling.
For that in the php file handing the upload, when a new photo is uploaded save a unix timestamp in a file:
Create another php file (ex: polling.php) that will handle AJAX calls and return the unix timestamp of the last upload:
In the Viewer javascript code make an AJAX polling that will detect a change in the timestamp and refresh the image when needed:
I haven't tested the code, so there might be mistakes but this is the idea.
You need to implement client long-polling connection with server what is called COMET or make use of socket if your browser supports websocket.
The best solution would be writing a small php script on the viewer web browser to periodically check the image and if its changed it will reload it. As the php code runs on server side you can do it easily. I hope this helps if you need code snippet let me know.
In javascript, there are a few ways to refresh programmatically.
The preferred method is
location.reload()
Another way is by setting the
location.href
property, the browser automatically goes to the new url, solocation=location
orlocation.href=location.href
would also do that.Though the second method probably looks weird.
To recap, that's:
I hope this helped.