mirror of
https://github.com/YGGverse/next.git
synced 2026-04-01 02:05:29 +00:00
implement multi-protocol async server based on ratchet library #1
This commit is contained in:
parent
296525aac3
commit
ec60ef5e5b
8 changed files with 707 additions and 387 deletions
263
src/Controller/Nex.php
Normal file
263
src/Controller/Nex.php
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
<?php
|
||||
|
||||
namespace Yggverse\Next\Controller;
|
||||
|
||||
use \Ratchet\MessageComponentInterface;
|
||||
|
||||
class Nex implements MessageComponentInterface
|
||||
{
|
||||
private \Yggverse\Next\Model\Environment $_environment;
|
||||
private \Yggverse\Next\Model\Filesystem $_filesystem;
|
||||
|
||||
public function __construct(
|
||||
\Yggverse\Next\Model\Environment $environment,
|
||||
\Yggverse\Next\Model\Filesystem $filesystem
|
||||
) {
|
||||
// Init environment
|
||||
$this->_environment = $environment;
|
||||
|
||||
// Init filesystem
|
||||
$this->_filesystem = $filesystem;
|
||||
|
||||
// Check port is defined
|
||||
if (!$this->_environment->get('port'))
|
||||
{
|
||||
// Set protocol defaults
|
||||
$this->_environment->set('port', 1900);
|
||||
}
|
||||
|
||||
// Dump event
|
||||
if ($this->_environment->get('dump'))
|
||||
{
|
||||
print(
|
||||
str_replace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{port}',
|
||||
'{root}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $this->_environment->get('host'),
|
||||
(string) $this->_environment->get('port'),
|
||||
(string) $this->_filesystem->root()
|
||||
],
|
||||
_('[{time}] [init] server started at {host}:{port}{root}')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function onOpen(
|
||||
\Ratchet\ConnectionInterface $connection
|
||||
) {
|
||||
// Dump event
|
||||
if ($this->_environment->get('dump'))
|
||||
{
|
||||
print(
|
||||
str_replace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{crid}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $connection->remoteAddress,
|
||||
(string) $connection->resourceId
|
||||
],
|
||||
_('[{time}] [open] incoming connection {host}#{crid}')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function onMessage(
|
||||
\Ratchet\ConnectionInterface $connection,
|
||||
$request
|
||||
) {
|
||||
// Define response
|
||||
$response = null;
|
||||
|
||||
// Filter request
|
||||
$request = trim(
|
||||
$request
|
||||
);
|
||||
|
||||
// Build absolute realpath
|
||||
$realpath = $this->_filesystem->absolute(
|
||||
$request
|
||||
);
|
||||
|
||||
// Make sure realpath valid to continue
|
||||
if ($this->_filesystem->valid($realpath))
|
||||
{
|
||||
// Route
|
||||
switch (true)
|
||||
{
|
||||
// File request
|
||||
case $file = $this->_filesystem->file($realpath):
|
||||
|
||||
// Return file content
|
||||
$response = $file;
|
||||
|
||||
break;
|
||||
|
||||
// Directory request
|
||||
case $list = $this->_filesystem->list($realpath):
|
||||
|
||||
// Try index file on defined
|
||||
if ($index = $this->_filesystem->file($realpath . $this->_environment->get('file')))
|
||||
{
|
||||
// Return index file content
|
||||
$response = $index;
|
||||
}
|
||||
|
||||
// Listing enabled
|
||||
else if ($this->_environment->get('list'))
|
||||
{
|
||||
// FS map
|
||||
$line = [];
|
||||
|
||||
foreach ($list as $item)
|
||||
{
|
||||
// Build gemini text link
|
||||
$link = ['=>'];
|
||||
|
||||
if ($item['name'])
|
||||
{
|
||||
$link[] = $item['file'] ? $item['name']
|
||||
: $item['name'] . '/';
|
||||
}
|
||||
|
||||
if ($item['time'] && $this->_environment->get('time'))
|
||||
{
|
||||
$link[] = date('Y-m-d', $item['time']);
|
||||
}
|
||||
|
||||
// Append link to the new line
|
||||
$line[] = implode(' ', $link);
|
||||
}
|
||||
|
||||
// Merge lines to response
|
||||
$response = implode(
|
||||
PHP_EOL,
|
||||
$line
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Dump event
|
||||
if ($this->_environment->get('dump'))
|
||||
{
|
||||
// Print debug from template
|
||||
print(
|
||||
str_ireplace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{crid}',
|
||||
'{path}',
|
||||
'{real}',
|
||||
'{size}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $connection->remoteAddress,
|
||||
(string) $connection->resourceId,
|
||||
(string) str_replace(
|
||||
'%',
|
||||
'%%',
|
||||
$request
|
||||
),
|
||||
(string) str_replace(
|
||||
'%',
|
||||
'%%',
|
||||
$realpath
|
||||
),
|
||||
(string) mb_strlen(
|
||||
$response
|
||||
)
|
||||
],
|
||||
_('[{time}] [message] incoming connection {host}#{crid} "{path}" > "{real}" {size} bytes')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
|
||||
// Noting to return?
|
||||
if (empty($response))
|
||||
{
|
||||
// Try failure file on defined
|
||||
if ($fail = $this->_filesystem->file($this->_environment->get('fail')))
|
||||
{
|
||||
$response = $fail;
|
||||
}
|
||||
}
|
||||
|
||||
// Send response
|
||||
$connection->send(
|
||||
$response
|
||||
);
|
||||
|
||||
// Disconnect
|
||||
$connection->close();
|
||||
}
|
||||
|
||||
public function onClose(
|
||||
\Ratchet\ConnectionInterface $connection
|
||||
) {
|
||||
// Dump event
|
||||
if ($this->_environment->get('dump'))
|
||||
{
|
||||
print(
|
||||
str_replace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{crid}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $connection->remoteAddress,
|
||||
(string) $connection->resourceId
|
||||
],
|
||||
_('[{time}] [close] incoming connection {host}#{crid}')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function onError(
|
||||
\Ratchet\ConnectionInterface $connection,
|
||||
\Exception $exception
|
||||
) {
|
||||
// Dump event
|
||||
if ($this->_environment->get('dump'))
|
||||
{
|
||||
print(
|
||||
str_replace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{crid}',
|
||||
'{info}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $connection->remoteAddress,
|
||||
(string) $connection->resourceId,
|
||||
(string) $exception->getMessage()
|
||||
],
|
||||
_('[{time}] [error] incoming connection {host}#{crid} reason: {info}')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
|
||||
// Disconnect
|
||||
$connection->close();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue