Adding Session with MemcacheSessionHandler

40 views
Skip to first unread message

ridwane...@gmail.com

unread,
May 4, 2015, 9:33:25 AM5/4/15
to ratch...@googlegroups.com
Iam trying to add user id use Session with MemcacheSessionHandler, but when onOpen the id not showed, this My code :

//Websocket
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Wamp\WampServer;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use \Ratchet\Session\SessionProvider;
use React\EventLoop\Factory;
use React\ZMQ\Context;
use React\Socket\Server;


echo "Start to listen...\n";

$pusher = new Pusher;
$loop   = Factory::create();

$context = new Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'notification'));


$memcache = new \Memcache();
$memcache->connect('127.0.0.1', 11211);
$memcacheHandler = new Handler\MemcacheSessionHandler($memcache);

$session = new SessionProvider(
    new WampServer(
        $pusher
    ),
    $memcacheHandler
);

$webSock = new Server($loop);
$webSock->listen(8080, '0.0.0.0');
$server = new IoServer(
    new HttpServer(
        new WsServer(
            $session
        )
    ),
    $webSock
);
$loop->run();
//class application
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
use Ratchet\MessageComponentInterface;

class Pusher implements WampServerInterface, MessageComponentInterface {

public function onOpen(ConnectionInterface $conn) {
//echo "Connection {$conn->resourceId} has connected\n";
echo "Connection {$conn->Session->get('userid')} has connected\n";
}

}

//website
$session = new Session();
$session->start();
$session->set('userid',"001");
//output commandline
Start to listen...
connection <blank> has connected
why in ouput  userid not showed ? what I missing ?

thanks

cboden

unread,
May 16, 2015, 12:27:28 AM5/16/15
to ratch...@googlegroups.com, ridwane...@gmail.com
If your WebSocket server is running on port 8080 I'm assuming your regular website is running on port 80. Web browsers treat different ports on the same domain as different domains and therefore will not send cookies to the WebSocket server. 

You either have to host your WebSocket server on a subdomain and configure PHP to send cookies to all subdomains or have your WebSocket server proxied through your web server so the browser accesses your website and your WebSocket server on port 80. 
Reply all
Reply to author
Forward
0 new messages