Swoole\WebSocket\Server->on('Start', fn)

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\WebSocket\Server->on('Start', callable $callback)

Parameters

event

The event name to set a callback for

callback

Callable function for the server event type

Return

If success, it returns true, otherwise it returns false

Description

This function is executed when the WebSocket server starts, this causes the event loop to trigger the Start event. You may perform startup tasks within this callback but do not perform any blocking operations at this stage.

Example

<?php

use Swoole\WebSocket\Server;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;

$server = new Server("127.0.0.1", 9501);

$server->on("Start", function(Server $server)
{
    echo "Swoole WebSocket Server started at 127.0.0.1:9501\n";
});

$server->on('Open', function(Server $server, Swoole\Http\Request $request)
{
    echo "connection open: {$request->fd}\n";

    $server->tick(1000, function() use ($server, $request)
    {
        $server->push($request->fd, json_encode(["hello", time()]));
    });
});

$server->on('Message', function(Server $server, Frame $frame)
{
    echo "received message: {$frame->data}\n";

    $server->push($frame->fd, json_encode(["hello", time()]));
});

$server->on('Close', function(Server $server, int $fd)
{
    echo "connection close: {$fd}\n";
});

$server->start();
Last updated on August 31, 2022