Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to monitor the status of an agent from the web. The agent opens a UDP socket and consistently send messages reflecting its status on a non-regular basis. I have a script,listen.pl, that opens a socket,binds it to the web-server IP and listen for incoming messages.

What I have in mind is to dynamically update a web page everytime a new message comes in.I've tried the following:


1)open a filehandle on listen.pl and use a while loop to print each line to html. I realize the web page wouldn't show until the while loop terminates,i.e. it cant refreshes line by line while each line of meassage comes in.


2)I modify listen.pl to take in one message and then close the socket. Doing this, the message appears. However ,it's not what I wanted. Next, I put everything in a subroutine and call the subroutine twice.By right, the first call would have generated the first message and then followed by the second.However, all I get is a long wait and the 2 messages pop out at the same time. I tried both including and excluding the html starting/ending tags in the subroutine.


Does anyone have any suggestion how to do this properly? Also,I need a way of letting the cgi script knows when the browser window is closed so that it will stop running listen.pl.Thanks

Replies are listed 'Best First'.
(crazyinsomniac) Re: Dynamic Updating of a HTML page
by crazyinsomniac (Prior) on Mar 22, 2001 at 12:59 UTC
    IndigoPerl(Win32) does something like this. Check out (crazyinsomniac) Re: IndigoPerl 5.6 for Win32 to see how it works, or just download it and play around.

    The basic jist of it that the script gathering the raw data keeps running and appending to an html file, while it redirects the user to a script that refreshes every 10 seconds, and displays the constantly updating html file, until the html file is not being updated any more(no more refresh)

    Oh yeah, they also have a binary called PerlConsole.exe which launches your default browser, along with apache, and monitors the browser for closing, so that when you do close it, perlconsole also closes apache.

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Dynamic Updating of a HTML page
by sutch (Curate) on Mar 22, 2001 at 13:09 UTC
    If I understand your question correctly, it sounds to me like you need two separate scripts: one to listen for incoming messages and log them and another to display the logs via http.

    To listen for incoming messages, you need a daemon that listens to the port and logs messages to a file. You also mention that you want the ability to start and stop the logging from the web. One possiblity for starting the daemon is by forking a process...see forking from web for some ideas. Maybe have a "last accessed via the web" timestamp in a file that the daemon checks occasionally and deletes the file and kills itself when the time is not recent enough.

    The web page would start the daemon, if not already started, parse the daemon's log file and display it as HTML. To refresh the page, use some JavaScript, HTTP refresh, or a META refresh (see Refreshing a Web page via Perl for some info).