diff --git a/modules/customModule/EventHandler.php b/modules/customModule/EventHandler.php index 0ec6eb2..77be5b2 100644 --- a/modules/customModule/EventHandler.php +++ b/modules/customModule/EventHandler.php @@ -10,19 +10,11 @@ class EventHandler { public static function onServerInit(ServerEvent $event) { - echo 'Server initialized' . PHP_EOL; + $event->server->echo('Server initialized'); } public static function onMessage(ConnectionMessageEvent $event) { - echo 'New message from ' . $event->connection->resourceId . ': ' . $event->message . PHP_EOL; - - $event->server->sendAllUsers([ - 'message', - [ - 'from_id' => $event->connection->resourceId, - 'text' => $event->message, - ], - ]); + $event->server->echo('New message from ' . $event->connection->resourceId . ': ' . $event->message); } } diff --git a/modules/customModule/models/CustomItem.php b/modules/customModule/models/CustomItem.php new file mode 100644 index 0000000..62a179f --- /dev/null +++ b/modules/customModule/models/CustomItem.php @@ -0,0 +1,19 @@ +eventManager->trigger($name, $event); } + /** + * @param string $message + * @param bool $prependDate + * @param bool $appendEol + */ + public function echo(string $message, $prependDate = true, $appendEol = true) + { + $result = ''; + + if ($prependDate) { + $ms = explode(' ', microtime()); + $ms = round((float)$ms[0], 3) * 1000; + + $result .= '[' . date('Y-m-d h:i:s', time()) . '.' . $ms . '] '; + } + + $result .= $message; + + if ($appendEol) { + $result .= PHP_EOL; + } + + echo $result; + } + } diff --git a/modules/server/components/CoreServer.php b/modules/server/components/CoreServer.php index 9754e14..d6d2a71 100644 --- a/modules/server/components/CoreServer.php +++ b/modules/server/components/CoreServer.php @@ -60,6 +60,7 @@ class CoreServer extends BaseServer public function onOpen(ConnectionInterface $connection) { if (!$this->authenticate($connection)) { + $connection->close(); return; } @@ -101,6 +102,8 @@ class CoreServer extends BaseServer */ public function onError(ConnectionInterface $conn, \Exception $e) { + $conn->close(); + $this->trigger(self::EVENT_CONNECTION_ERROR, new ConnectionErrorEvent([ 'server' => $this, 'connection' => $conn, diff --git a/modules/server/interfaces/ItemInterface.php b/modules/server/interfaces/ItemInterface.php new file mode 100644 index 0000000..07815bd --- /dev/null +++ b/modules/server/interfaces/ItemInterface.php @@ -0,0 +1,17 @@ +hasOne(Device::className(), ['id' => 'device_id']); + } + + /** + * @inheritdoc + */ + public function init() + { + $this->type = static::getType(); + + parent::init(); + } + + /** + * @throws NotSupportedException + */ + public static function find() + { + throw new NotSupportedException('Item query is not defined'); + } + + /** + * @inheritdoc + */ + public function beforeSave($insert) + { + $this->type = static::getType(); + + return parent::beforeSave($insert); + } + +} diff --git a/modules/server/models/query/BaseItemQuery.php b/modules/server/models/query/BaseItemQuery.php new file mode 100644 index 0000000..33421e7 --- /dev/null +++ b/modules/server/models/query/BaseItemQuery.php @@ -0,0 +1,19 @@ +type !== null) { + $this->andWhere(['type' => $this->type]); + } + + return parent::prepare($builder); + } +}