I’m trying to get started with OpenSwoole and I’m using the built-in UDP server. I’d like to push any received messages into a channel so that another coroutine can process them. I’ve Googled extensively and can’t find an obvious way of doing it.
This is the code that I currently have:
// Create a channel with space for 1000 messages
$chan = new OpenSwooleCoroutineChannel(1000);
co::run(function () use ($chan){
// Listener
go(function() use ($chan) {
// Read from channel
while (1) {
$data_read = $chan->pop();
echo ("Data read: $data_readn");
}
});
});
// UDP server
// Set coroutine options before the server is started
OpenSwooleCoroutine::set([
'enable_coroutine' => true
]);
// Start a new UDP server on 12.0.0.1, listening on port 9502
$server = new OpenSwooleServer('127.0.0.1', 9502, OpenSwooleServer::POOL_MODE, OpenSwooleConstant::SOCK_UDP);
// Setup the incoming data event callback, called 'Packet'
$server->on('Packet', function ($server, $data, $clientInfo) use ($chan)
{
echo ("Data written: $datan");
$chan->push($data);
});
// Start the server and begin accepting incoming requests
$server->start();
But when I run it I get the error:
===================================================================
[FATAL ERROR]: all coroutines (count: 1) are asleep - deadlock!
===================================================================
How do I wrap the UDP server up into a coroutine ? I thought that it already ran as one or have I missed something obvious ?