OpenSwoole\Coroutine\Server->handle()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\Server->handle(callable $callback): void

Parameters

callback

The main processing function for when a connection is received to the server. This callback handles server requests. Check the example on its usage and parameters.

Return

None


Description

The main processing function used for when successful connections are received.

You must set this function before starting the server.

When a connection is successful, when the callback is called to handle the request, the server will have already created a coroutine for you, no need to create one yourself.


Example

<?php

$server = new OpenSwoole\Coroutine\Server('127.0.0.1', 8080);

$server->handle(function(OpenSwoole\Coroutine\Server\Connection $connection)
{
    while(true)
    {
      $data = $connection->recv();
    }
});

...

This callback is given a OpenSwoole\Coroutine\Server\Connection object to allow you to process the request and access information about it. You can use $connection->exportSocket() to get the underlying UnixSocket object if you need to.

Last updated on September 1, 2022