Answer the question
In order to leave comments, you need to log in
Why are sessions not transmitted to the websocket server?
Why sessions may not be transmitted to the websocket server? When authorizing on the site, I save the user's data in the session, then I need to transfer them to the websocket server, but no matter how I try, the sessions are empty.
server.php code:
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/chat/vendor/autoload.php';
$server = IoServer::factory(new HttpServer(new WsServer(new Chat()), 8080));
$server->run();
<?php
namespace MyApp;
require "D:/OpenServer/OSPanel/domains/App/db.php";
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use MyApp\Entities\Message;
if(isset($_SESSION['logged_user']->login)) {
echo "true" ;
}
else {
echo "false ";
}
//код...
Answer the question
In order to leave comments, you need to log in
Taaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, well, what do you think the sessions are fixed to? Let me remind you that when an http request comes to the web server, it creates a new puff process and palms off the necessary data to it. Your websocket server is the only process that never dies, and no one slips anything into it. Question: how does he know what kind of data you want to receive from him?
You will have to write a separate authorization system for the socket server, i.e. at the front, pass some parameter during / after connecting to the socket server, containing information with which you can securely identify the user (cookie, jwt, some api key), after which the socket server must somehow check this information and somewhere to save the information (in memory) that "such and such a connection came from such and such a user."
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question