mirror of
https://github.com/YGGverse/next.git
synced 2025-03-12 13:41:27 +00:00
init common server type driver
This commit is contained in:
parent
d1c25ef574
commit
207b5f577a
139
src/Abstract/Type/Nex.php
Normal file
139
src/Abstract/Type/Nex.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Yggverse\Next\Abstract\Type;
|
||||
|
||||
use \Ratchet\MessageComponentInterface;
|
||||
|
||||
abstract class Nex implements MessageComponentInterface
|
||||
{
|
||||
protected \Yggverse\Next\Model\Environment $_environment;
|
||||
|
||||
abstract public function init();
|
||||
|
||||
public function __construct(
|
||||
\Yggverse\Next\Model\Environment $environment
|
||||
) {
|
||||
if (!$environment->get('port')) $environment->set('port', 1900);
|
||||
|
||||
if ($environment->get('dump'))
|
||||
{
|
||||
print(
|
||||
str_replace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{port}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $environment->get('host'),
|
||||
(string) $environment->get('port')
|
||||
],
|
||||
_('[{time}] [construct] server {host}:{port}')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
|
||||
$this->_environment = $environment;
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
public function onOpen(
|
||||
\Ratchet\ConnectionInterface $connection
|
||||
) {
|
||||
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
|
||||
) {
|
||||
if ($this->_environment->get('dump'))
|
||||
{
|
||||
print(
|
||||
str_replace(
|
||||
[
|
||||
'{time}',
|
||||
'{host}',
|
||||
'{crid}'
|
||||
],
|
||||
[
|
||||
(string) date('c'),
|
||||
(string) $connection->remoteAddress,
|
||||
(string) $connection->resourceId
|
||||
],
|
||||
_('[{time}] [message] incoming connection {host}#{crid}')
|
||||
) . PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function onClose(
|
||||
\Ratchet\ConnectionInterface $connection
|
||||
) {
|
||||
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
|
||||
) {
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
$connection->close();
|
||||
}
|
||||
}
|
@ -2,29 +2,24 @@
|
||||
|
||||
namespace Yggverse\Next\Controller\Nex;
|
||||
|
||||
use \Ratchet\MessageComponentInterface;
|
||||
|
||||
class Filesystem implements MessageComponentInterface
|
||||
class Filesystem extends \Yggverse\Next\Abstract\Type\Nex
|
||||
{
|
||||
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;
|
||||
public function init()
|
||||
{
|
||||
// Validate environment arguments defined for this type
|
||||
if (!$this->_environment->get('root'))
|
||||
{
|
||||
throw new \Exception(
|
||||
_('filesystem root path required!')
|
||||
);
|
||||
}
|
||||
|
||||
// Init filesystem
|
||||
$this->_filesystem = $filesystem;
|
||||
|
||||
// Check port is defined
|
||||
if (!$this->_environment->get('port'))
|
||||
{
|
||||
// Set protocol defaults
|
||||
$this->_environment->set('port', 1900);
|
||||
}
|
||||
$this->_filesystem = new \Yggverse\Next\Model\Filesystem(
|
||||
$this->_environment->get('root')
|
||||
);
|
||||
|
||||
// Dump event
|
||||
if ($this->_environment->get('dump'))
|
||||
@ -49,30 +44,6 @@ class Filesystem implements MessageComponentInterface
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@ -214,58 +185,4 @@ class Filesystem implements MessageComponentInterface
|
||||
// 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…
x
Reference in New Issue
Block a user