Swoole\Coroutine\Server->handle()

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\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 Swoole\Coroutine\Server('127.0.0.1', 8080);

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

...

This callback is given a Swoole\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 August 31, 2022