Swoole DTLS for PHP

Version: Swoole: 4.5.0+

Swoole DTLS Server

DTLS stands for Datagram Transport Layer Security. Simply put, DTLS is UDP + security. Swoole support DTLS from version 4.5.

To enable DTLS at UDP server side, you can use SWOOLE_SOCK_UDP | SWOOLE_SSL.

Example:

<?php

$server = new Swoole\Server('0.0.0.0', 5000, SWOOLE_BASE, SWOOLE_SOCK_UDP | SWOOLE_SSL);

$server->set([
    'ssl_cert_file' => __DIR__ . '/../ssl/ssl.crt',
    'ssl_key_file' => __DIR__ . '/../ssl/ssl.key',
]);

$server->on('receive', function(Swoole\Server $serv, $fd, $tid, $data) {
    //var_dump($fd, $data, $serv->getClientInfo($fd));
    $serv->send($fd, "echo: $data\n");
});

$server->start();

Swoole DTLS Client

To enable DTLS at UDP client side, you can use SWOOLE_SOCK_UDP | SWOOLE_SSL.

Example:

<?php
Co\run(function() {
    $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP | SWOOLE_SSL);
    if (!$client->connect('127.0.0.1', 5000)) {
        exit("connect failed\n");
    }
    $client->send("hello world");
});