1. Go to this page and download the library: Download loner/stream-stateless library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
loner / stream-stateless example snippets
#!/usr/bin/env php
declare(strict_types=1);
use Loner\Reactor\Builder;
use Loner\Stream\Event\{Start, Stop};
use Loner\Stream\Stateless\{Event\Accept, Server\UdpServer, ServerEvent};
// composer 自加载
= new UdpServer($reactor, '0.0.0.0', 6957);
// 绑定事件响应
$server
// 服务器启动(创建监听网络后、进入事件循环前)
->on(ServerEvent::Start, function (Start $event) {
echo sprintf('Start: %s', $event->server->getSocketAddress()), PHP_EOL;
})
// 服务器停止(移除监听网络后、破坏事件循环前)
->on(ServerEvent::Stop, function (Stop $event) {
echo sprintf('Stop: %s', $event->server->getSocketAddress()), PHP_EOL;
})
// 收到远程消息
->on(ServerEvent::Accept, function (Accept $event) {
echo sprintf('Accept: %s => %s', $event->remoteAddress, $event->message), PHP_EOL;
$event->server->send('Hi. I\'m the server.', $event->remoteAddress);
});
// 启动服务器
$server->start();