Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Listener in PHP

I want my web server to notify me through a php page when an event occurs at another TCP server, to which the PHP page has successfully connected via a socket. The event is like the TCP server wants to send a message to the web server, etc. Is there any way to accomplish this and/or any references on how to do it?

like image 217
Izza Avatar asked Dec 15 '25 11:12

Izza


1 Answers

Sure:

$fp = fsockopen("tcp://example.com", 8888) OR die("could not connect");
while (!feof($fp)) {
    $pc = fread($handle, 8192);
    if ($pc === false || strlen($pc) == 0)
        break;
    //a new packet has arrived
    //you should collect the read in a variable and wait
    //for another packet until you know the message is complete
    //(depends on the protocol)
    collect_in_result($pc);
    if (message_is_complete()) {
        if (check_event()) {
            //take action
        }
    }
}
like image 87
Artefacto Avatar answered Dec 17 '25 02:12

Artefacto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!