简体   繁体   中英

How to create asynchronous socket server using php?

I'm trying to create a Asynchronous socket server to handle socket client, send message to client when it's needs. I use reactphp library to implement it but I can't do correct, my server still is blocked. I'm new in this library, please help.

require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {
    $conn->pipe($conn);
});
echo "Socket server listening on port 4000.\n";
echo "You can connect to it by running: telnet localhost 4000\n";
$socket->listen(4200);
$loop->run();


// code bottom doesn't run because of blocking socket

$loop->run(); runs the event loop and will never return unless you stop the loop. The loop is your scheduler and invokes your event handlers in case events occur.

If you want to react to events, you have to register event listeners before you run the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM