jerryb Posted October 12, 2015 Share Posted October 12, 2015 Hello Folks!I am new here so forgive me if I do not correctly follow your protocol.I have created a live chat on my website. The chat works very well. However, I am attempting to add an 'alarm' of sorts that will indicate when someone either responds to the chat, or enters the chat. I am attempting to create a setTimeout script that repeats every three seconds. At the beginning of the script I generate the length of the chat log file, decide whether the file is larger than before, then play a sound file if the file is larger than before. The script just keeps repeating the sound file. My code is as follows... <script> (function(){ <?php $handle = fopen("log.html", "r"); $contents = fread($handle,filesize("log.html")); fclose($handle); $strnew = strlen ($contents); if ($strnew > $strold) { ?> var aSound = document.createElement('audio'); aSound.setAttribute('src', '/sounds/door_chime.mp3'); aSound.play(); <?php } $strold = $strnew; ?> setTimeout(arguments.callee, 3000); }) (); </script> Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/ Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 You can't do that. All of the PHP is executed before the JS even exists. You can't loop or re-call PHP from JS. You'd have to use an AJAX request. But what you're doing is crazy inefficient. To create a live chat you want to be using WebSockets, which create a persistent TCP connection between client and server. It is easily possible to achieve the alert with WebSockets because it is possible for all clients to know exactly when a message was pushed. Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523051 Share on other sites More sharing options...
jerryb Posted October 12, 2015 Author Share Posted October 12, 2015 I appreciate that response, but it appears that even they are still working on the technology WEB SOCKETSTHIS IS AN EXPERIMENTAL TECHNOLOGYBecause this technology's specification has not stabilized, check the compatibility for the proper prefixes to use in varius browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specs change. Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523060 Share on other sites More sharing options...
Jacques1 Posted October 12, 2015 Share Posted October 12, 2015 Just because Web Sockets haven't been formally standardized yet doesn't mean you cannot use them. The only problem would be legacy browsers. However, I'd still stick to your current approach (polling). Sure, it's not super-efficient, but who cares when you clearly aren't running a big professional website? Making a request every few seconds is perfectly fine. Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523061 Share on other sites More sharing options...
jerryb Posted October 12, 2015 Author Share Posted October 12, 2015 Thanks Guru,However, I can't figure how to get the two variables, compare them and then fire the sound file. In my example above the sound simply loops every 3 seconds. Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523062 Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 You can't, it's not possible to execute PHP with Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523063 Share on other sites More sharing options...
jerryb Posted October 13, 2015 Author Share Posted October 13, 2015 (edited) Well, after some research and a bit of thought, I came up with a way to make this work.... $handle = fopen("log.html", "r"); $contents = fread($handle,filesize("log.html")); fclose($handle); $strnew = strlen ($contents); $strold = $_COOKIE['strold']; if ($strnew > $strold) {?> <script> var aSound = document.createElement('audio'); aSound.setAttribute('src', '/sounds/door_chime.mp3'); aSound.play(); </script> <? setcookie("strold", $strnew); } The only problem I am having now is that this all works through a refresh code... <meta http-equiv="refresh" content="30"> This causes problems if the refresh happens during a time when the user is filling in the comment form. Any ideas on how I could possibly reload the entire page once the user submits the comments through the form submit? Edited October 13, 2015 by jerryb Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523180 Share on other sites More sharing options...
scootstah Posted October 13, 2015 Share Posted October 13, 2015 You need to use either AJAX or WebSockets as mentioned earlier. The AJAX would be set to run on an interval and refresh the chat in the browser with what the server sends it. No need to do a full page refresh. Quote Link to comment https://forums.phpfreaks.com/topic/298554-help-with-using-php-within-a-script/#findComment-1523182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.