So I am learning about javascript, so I am making a live chat system with PHP and javascript. I have it so the javascript refreshes the log (each message gets logged in a file on the server), and it refreshes every second. Im using firebug to monitor the resource usage, and I see under the net tab each times its updated, and the bytes add up really fast. I know I can change it to update less, but is there a way that wh开发者_如何学运维en a user on the other end I'm talking to, when the send a message, it gets sent to the server, then an alert gets sent to me saying that the chatlog needs to update somehow. That way it only updates when the log is updated. let me know, thanks
You may be able to work with something akin to long polling.
You could timestamp the messages, and with each client 'refresh', check to see if there are any new messages and only send those.
If you want to use less bandwidth consider using GZip/Deflate compression. The requests themselves aren't very significant... the payload however, is.
There are two ways of doing compression, either through code in the backend or through the web server. You can use Google to find articles on how to enable compression for your specific web server as well as code examples for compression in PHP.
Also make the delay longer as you indicated in the original post. Compression and longer delay will make a worthy difference.
Try the easiest things first, if neither of my suggestions help you can always explore alternative options.
Heres a code example for gzip -C http://www.webcodingtech.com/php/gzip-compression.php
精彩评论