websocket changes

This commit is contained in:
Yuri Kuznetsov
2025-09-25 12:06:34 +03:00
parent aaf30cf694
commit fa85d9e132
5 changed files with 161 additions and 2 deletions
@@ -29,10 +29,12 @@
namespace Espo\Core\WebSocket;
use Espo\Core\Utils\Json;
use GuzzleHttp\Psr7\Query;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\ServerProtocol as WAMP;
use Ratchet\Wamp\Topic;
use Ratchet\Wamp\WampConnection;
use Ratchet\Wamp\WampServerInterface;
@@ -408,6 +410,8 @@ class Pusher implements WampServerInterface
}
$this->subscribeUser($conn, $userId);
$this->sendWelcome($conn);
});
}
@@ -572,4 +576,15 @@ class Pusher implements WampServerInterface
$this->topicHash[$topicId] = $topic;
}
private function sendWelcome(ConnectionInterface $conn): void
{
/**
* @noinspection PhpPossiblePolymorphicInvocationInspection
* @phpstan-ignore property.notFound
*/
$sessionId = $conn->WAMP->sessionId;
$conn->send(Json::encode([WAMP::MSG_WELCOME, $sessionId, 1]));
}
}
@@ -0,0 +1,48 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\WebSocket\Ratchet;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\ServerProtocol;
class EspoServerProtocol extends ServerProtocol
{
/**
* @inheritDoc
* @phpstan-ignore missingType.return
*/
public function onOpen(ConnectionInterface $conn)
{
$decor = new EspoWampConnection($conn);
$this->connections->attach($conn, $decor);
$this->_decorating->onOpen($decor);
}
}
@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\WebSocket\Ratchet;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampConnection;
use stdClass;
class EspoWampConnection extends WampConnection
{
/**
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(ConnectionInterface $conn)
{
$this->wrappedConn = $conn;
$this->WAMP = new stdClass;
$this->WAMP->sessionId = str_replace('.', '', uniqid((string) mt_rand(), true));
$this->WAMP->prefixes = [];
}
}
@@ -0,0 +1,48 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\WebSocket\Ratchet;
use Ratchet\Wamp\TopicManager;
use Ratchet\Wamp\WampServer;
use Ratchet\Wamp\WampServerInterface;
/**
* Customized so that the welcome response is not sent on open. It will be sent after async access check.
*/
class EspoWampServer extends WampServer
{
/**
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(WampServerInterface $app)
{
$this->wampProtocol = new EspoServerProtocol(new TopicManager($app));
}
}
@@ -38,7 +38,6 @@ use React\Socket\SecureServer as SocketSecureServer;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Wamp\WampServer;
/**
* Starts a web-socket server.
@@ -90,7 +89,7 @@ class ServerStarter
$socketServer = new SocketSecureServer($socketServer, $loop, $sslParams);
}
$wsServer = new WsServer(new WampServer($pusher));
$wsServer = new WsServer(new Ratchet\EspoWampServer($pusher));
$wsServer->enableKeepAlive($loop, 60);
new IoServer(