Swoole\Coroutine\Server->handle()

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.