[英]How can I use legacy sessions ($_SESSION) with symfony sessions in Ratchet websocket application
好吧,让我首先说我不是框架的忠实拥护者。 我已经使用旧版会话(login.php)开发了我的应用程序:
<?php
$_SESSION{'user_type'} = 'u';
$_SESSION{'id'} = $q[0]['id'];
$_SESSION{'email'} = $q[0]['email'];
$_SESSION{'username'} = $q[0]['username'];
$_SESSION{'firstname'} = $q[0]['firstname'];
$_SESSION{'lastname'} = $q[0]['lastname'];
$_SESSION{'about_me'} = $q[0]['about_me'];
$_SESSION{'gender'} = $q[0]['gender'];
现在,这可以正常工作,用户可以登录并访问其个人资料,但是现在我正在尝试实现websocket。 所有教程仅向您展示如何在没有会话的情况下实现它,但是我想实现一个通知系统,当一个用户做某事(例如添加另一家公司发布的商品)时,我希望该特定公司得到通知。 这是我在实现websockets(server.php)时在终端上运行的代码:
<?php
require 'vendor/autoload.php';
require 'include/connect.php';
include 'models/Notify.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Session\SessionProvider;
//use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
use Ratchet\App;
use Notify\Notification;
try{
$domain = '127.0.0.1';
$storage = new PhpBridgeSessionStorage();
$session = new Session($storage);
$session->migrate();
$server = IoServer::factory(
new HttpServer(
new SessionProvider(
new WsServer(
new Notification
),
new \SessionHandler
)
),
3002,
$domain
);
$server->run();
}catch(Exception $e){
echo "Error: {$e->getMessage()}";
}
当我尝试访问会话变量时,会话为空。 在使用PhpBridgeSessionStorage
https://symfony.com/doc/current/session/php_bridge.html时,我遵循了symfony的文档,但是不清楚。 这是我在Notification
类(Notify.php)中使用的代码:
<?php
namespace Notify;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
class Notification implements MessageComponentInterface{
protected $clients;
private $users;
function __construct(){
$this->clients = new \SplObjectStorage;
$this->users = [];
}
public function onOpen(ConnectionInterface $conn){
echo "Connected ({$conn->resourceId})\n";
//$conn->Session->start();
var_dump($conn->Session->all());
}
public function onMessage(ConnectionInterface $from, $msg){
//echo "$msg [{$from->Session->get('username')}]\n";
}
public function onClose(ConnectionInterface $conn){
echo "\nConnection Closed ({$conn->resourceId})\n";
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e){
}
}
当我在终端上输出$conn->Session->all()
时,我得到一个警告PHP Warning: SessionHandler::open(): Session is not active
并带有空数组,当我将$_SESSION
输出到终端时,我得到了:
array(3) {
["_sf2_attributes"]=>
&array(0) {
}
["_symfony_flashes"]=>
&array(0) {
}
["_sf2_meta"]=>
&array(3) {
["u"]=>
int(1518882401)
["c"]=>
int(1518882401)
["l"]=>
string(1) "0"
}
}
我越来越没有会话数据,我已经使用其他会话处理程序就像试图MemcacheSessionHandler, MemcachedSessionHandler, PdoSessionHandler
但他们都离开了会议空的,我试过其他存储类,但symfony的文档建议使用PhpBridgeSessionStorage
当你想传统的会话与集成symfony会议。
我知道那里有用于推送通知的框架,但是我不想使用它们的第三方服务。
我已经研究了好几天,但找不到任何有用的东西。
如果您不喜欢像我这样的框架,那么我猜这个答案可以帮助您。 实际上,我找不到关于如何在WebSocket上将Symfony会话与旧版会话($ _SESSION)结合使用的良好文档。 大多数教程和主题建议仅在整个应用程序中使用Symfony会话。但是,如果您像我一样并且已经在大多数应用中使用了旧式会话,则可以尝试这种替代方法。
摆脱您的symfony会话代码和棘轮会话提供程序,这就是我的server.php现在的样子:
<?php
require 'vendor/autoload.php';
require 'include/connect.php';
include 'models/Notify.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\App;
use Notify\Notification;
try{
$domain = '127.0.0.1';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Notification
)
),
3002,
$domain
);
$server->run();
}catch(Exception $e){
echo "Error: {$e->getMessage()}";
}
接下来需要做的是,每当客户端连接到WebSocket服务器时,就需要获取其PHPSESSID。 您可以在我的案例Notification类中从您的应用程序类中的连接对象获取。 执行$conn->httpRequest->getHeader('Cookie')
将返回一个cookie数组,因此,您需要编写代码来循环并获取PHPSESSID,如下所示:
function getPHPSESSID(array $cookies){
foreach($cookies as $cookie):
if(strpos($cookie, "PHPSESSID") == 0):
$sess_id = explode('=', $cookie)[1];
break;
endif;
endforeach;
return $sess_id;
}
有了它后,您可以使用它通过设置会话ID session_id($sessid)
来在websocket服务器上设置会话,但是为了避免我遇到的麻烦,我建议您在用户登录后将PHPSESSID存储在数据库中。 ..该ID应该是唯一的,为防止冲突,请不要使用session_regenerate_id()
,而应使用session_create_id()
。 因此,在WebSocket服务器上,您将运行查询以获取具有PHPSESSID的用户的信息。 我建议像执行此操作那样创建Session类:
<?php
namespace Library;
class _Session {
private $sess_id;
private $session;
public function __construct(array $cookies){
include 'include/connect.php';
foreach($cookies as $cookie):
if(strpos($cookie, "PHPSESSID") == 0):
$this->sess_id = explode('=', $cookie)[1];
break;
endif;
endforeach;
$query = "SELECT * FROM general_user WHERE sess_id = ?";
$query_run = $database->prepare($query);
if($query_run->execute([$this->sess_id])):
if($query_run->rowCount() == 1):
$query = "SELECT g.id, g.email, u.username, u.firstname, u.lastname, g.about_me, u.gender, p.name AS profile_pic, 'u' AS user_type FROM general_user g INNER JOIN userdata u ON u.id = g.id LEFT JOIN profile_photo p ON g.id = p.user_id WHERE sess_id = ?";
$query_run = $database->prepare($query);
if($query_run->execute([$this->sess_id])):
if($query_run->rowCount() == 0):
$query = "SELECT g.id, g.email, g.about_me AS company_description, c.category, c.subcategory, c.company_name, c.contact_num, c.website, p.name AS profile_pic, 'c' AS user_type FROM general_user g INNER JOIN companydata c ON c.id = g.id LEFT JOIN profile_photo p ON g.id = p.user_id WHERE sess_id = ?";
$query_run = $database->prepare($query);
if($query_run->execute([$this->sess_id])):
$this->session = $query_run->fetchAll()[0];
endif;
else:
$this->session = $query_run->fetchAll()[0];
endif;
endif;
else:
$this->session = [];
endif;
endif;
}
function get($key){
foreach($this->session as $k => $value)
if($key == $k)
return $value;
throw new \Exception("Undefined index $key");
}
}
因此,在您提供的“ Cookie”数组中,当您在应用类中创建会话对象时,您将获得有关登录用户的信息:
<?php
namespace Notify;
require __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../classes/CustomSession.php';
use Library\_Session;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
class Notification implements MessageComponentInterface{
protected $clients;
function __construct(){
$this->clients = [];
echo "WebSocket Server Running...\n";
}
public function onOpen(ConnectionInterface $conn){
$session = new _Session($conn->httpRequest->getHeader('Cookie'));//My custom session
if($session->get('user_type') == 'u'){
echo $session->get('username')." has connected\n";
}else{
echo $session->get('company_name')." has connected\n";
}
array_push($this->clients, ['connection' => $conn, 'session'=> $session]);
}
}
对于具有旧式会话的Symfony会话,这是我能想到的最佳选择。 随意问的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.