Session not working.

735 views
Skip to first unread message

chr...@gmail.com

unread,
Jun 23, 2013, 11:08:38 AM6/23/13
to ratch...@googlegroups.com
Hi,
Sharing session between http and websocket isn't working :(

My index.php file:
<?php
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
require 'vendor/autoload.php';
require('libs/Smarty.class.php');
$smarty = new Smarty;

$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$storage = new NativeSessionStorage(array(), new MemcacheSessionHandler($memcache));

$session = new Session();

$session->start();
$session->set('name', 'Drak');
print_r($session->all());

//$smarty->force_compile = true;
$smarty->debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;


$smarty->display('index.tpl');
?>

my pickup-server.php file:

<?php
 
use Ratchet\Session\SessionProvider;
 
use Ratchet\WebSocket\WsServer;
 
use Ratchet\Server\IoServer;
 
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
 
use WebPickup\Pickup;


 
require '../vendor/autoload.php';


 $memcache
= new Memcache;
 $memcache
->connect('localhost', 11211);


 $session
= new SessionProvider(
 
new Pickup(),
 
new Handler\MemcacheSessionHandler($memcache)
 
);


 $server
= IoServer::factory(new WsServer($session), 8080);


 $server
->run();

my pickup.php file:

<?php
namespace WebPickup;
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket;
use Ratchet\ConnectionInterface;
use Ratchet\Session\Serialize\HandlerInterface;


class Pickup implements MessageComponentInterface {
 
protected $clients;


 
public function __construct() {
 $this
->clients = new \SplObjectStorage;
 
}


 
public function onOpen(ConnectionInterface $conn) {
      $this
->clients->attach($conn);
 print_r
($conn->Session->all());
 
}


 
public function onMessage(ConnectionInterface $from, $msg) {1;
 $newmsg
= mb_substr($msg,6);
 
if($newmsg != "")
 
{
 echo sprintf
('Connection %d sending chat message "%s" to %d other connection%s' . "\n"
 
, $from->resourceId, $newmsg, $numRecv, $numRecv == 1 ? '' : 's');


 
foreach ($this->clients as $client) {
 $client
->send($newmsg);
 
}
 
}
}


 
public function onClose(ConnectionInterface $conn) {
 
// The connection is closed, remove it, as we can no longer send it messages
 $this
->clients->detach($conn);


 echo
"Connection {$conn->resourceId} has disconnected\n";
 
}


 
public function onError(ConnectionInterface $conn, \Exception $e) {
 echo
"An error has occurred: {$e->getMessage()}\n";


 $conn
->close();
 
}
 
 
public function onLogin($entry) {
 
print $entry;
   
}
}



cboden

unread,
Jun 23, 2013, 2:43:14 PM6/23/13
to ratch...@googlegroups.com
Hi Kuba,

Are your web and websocket clients connecting to the same domain for your website and Ratchet app? It's possible your'e receiving an empty session because the cookie isn't being sent. 

In your onOpen check your cookie is the same as your HTTP requests:  var_dump($conn->WebSocket->request->getCookies());

Cheers.

Kuba Chronowski

unread,
Jun 24, 2013, 3:55:21 AM6/24/13
to ratch...@googlegroups.com
They are on same domain and even on the same server.

It's tryhardhosting.com so I set session_cookie_domain to ".tryhardhosting.com" and it's not working :(
$conn->WebSocket->request->getCookies() returns empty array :(
Should I set memcache host to something other than localhost or is it good?

Kuba Chronowski

unread,
Jun 24, 2013, 4:03:07 AM6/24/13
to ratch...@googlegroups.com
That's my memcache log:
slab class   1: chunk size        96 perslab   10922
slab class   2: chunk size       120 perslab    8738
slab class   3: chunk size       152 perslab    6898
slab class   4: chunk size       192 perslab    5461
slab class   5: chunk size       240 perslab    4369
slab class   6: chunk size       304 perslab    3449
slab class   7: chunk size       384 perslab    2730
slab class   8: chunk size       480 perslab    2184
slab class   9: chunk size       600 perslab    1747
slab class  10: chunk size       752 perslab    1394
slab class  11: chunk size       944 perslab    1110
slab class  12: chunk size      1184 perslab     885
slab class  13: chunk size      1480 perslab     708
slab class  14: chunk size      1856 perslab     564
slab class  15: chunk size      2320 perslab     451
slab class  16: chunk size      2904 perslab     361
slab class  17: chunk size      3632 perslab     288
slab class  18: chunk size      4544 perslab     230
slab class  19: chunk size      5680 perslab     184
slab class  20: chunk size      7104 perslab     147
slab class  21: chunk size      8880 perslab     118
slab class  22: chunk size     11104 perslab      94
slab class  23: chunk size     13880 perslab      75
slab class  24: chunk size     17352 perslab      60
slab class  25: chunk size     21696 perslab      48
slab class  26: chunk size     27120 perslab      38
slab class  27: chunk size     33904 perslab      30
slab class  28: chunk size     42384 perslab      24
slab class  29: chunk size     52984 perslab      19
slab class  30: chunk size     66232 perslab      15
slab class  31: chunk size     82792 perslab      12
slab class  32: chunk size    103496 perslab      10
slab class  33: chunk size    129376 perslab       8
slab class  34: chunk size    161720 perslab       6
slab class  35: chunk size    202152 perslab       5
slab class  36: chunk size    252696 perslab       4
slab class  37: chunk size    315872 perslab       3
slab class  38: chunk size    394840 perslab       2
slab class  39: chunk size    493552 perslab       2
slab class  40: chunk size    616944 perslab       1
slab class  41: chunk size    771184 perslab       1
slab class  42: chunk size   1048576 perslab       1
<26 server listening (auto-negotiate)
<27 send buffer was 212992, now 268435456
<27 server listening (udp)
<27 server listening (udp)
<27 server listening (udp)
<27 server listening (udp)
<28 new auto-negotiating client connection
<29 new auto-negotiating client connection
<29 connection closed.
SIGINT handled.

Seshachalam Malisetti

unread,
Oct 15, 2013, 12:59:26 AM10/15/13
to ratch...@googlegroups.com
Hi Cboden,

If am using pdo session handler of symfony and i have the problem of sharing sessions between web server and web socket server. If i print the $conn->WebSocket->request->getCookies() i am getting the "array(0) {}" empty array. is it because of the cookie. how i can send the cookie ? if i print the $conn->session i am receiving the below output. Symfony\Component\HttpFoundation\Session\Session Object
(
    [storage:protected] => Ratchet\Session\Storage\VirtualSessionStorage Object
        (
            [_serializer:protected] => Ratchet\Session\Serialize\PhpHandler Object
                (
                )

            [bags:protected] => Array
                (
                    [attributes] => Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag Object
                        (
                            [name:Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag:private] => attributes
                            [storageKey:Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag:private] => _sf2_attributes
                            [attributes:protected] => Array
                                (
                                )

                        )

                    [flashes] => Symfony\Component\HttpFoundation\Session\Flash\FlashBag Object
                        (
                            [name:Symfony\Component\HttpFoundation\Session\Flash\FlashBag:private] => flashes
                            [flashes:Symfony\Component\HttpFoundation\Session\Flash\FlashBag:private] => Array
                                (
                                )

                            [storageKey:Symfony\Component\HttpFoundation\Session\Flash\FlashBag:private] => _sf2_flashes
                        )

                )

            [started:protected] => 
            [closed:protected] => 
            [saveHandler:protected] => Ratchet\Session\Storage\Proxy\VirtualProxy Object
                (
                    [_sessionId:protected] => 
                    [_sessionName:protected] => PHPSESSID
                    [handler:protected] => Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler Object
                        (
                        )

                    [wrapper:protected] => 
                    [active:protected] => 
                    [saveHandlerName:protected] => user
                )

            [metadataBag:protected] => Symfony\Component\HttpFoundation\Session\Storage\MetadataBag Object
                (
                    [name:Symfony\Component\HttpFoundation\Session\Storage\MetadataBag:private] => __metadata
                    [storageKey:Symfony\Component\HttpFoundation\Session\Storage\MetadataBag:private] => _sf2_meta
                    [meta:protected] => Array
                        (
                            [c] => 0
                            [u] => 0
                            [l] => 0
                        )

                    [lastUsed:Symfony\Component\HttpFoundation\Session\Storage\MetadataBag:private] => 
                )

        )

    [flashName:Symfony\Component\HttpFoundation\Session\Session:private] => flashes
    [attributeName:Symfony\Component\HttpFoundation\Session\Session:private] => attributes
)

Please help.

cboden

unread,
Oct 15, 2013, 10:42:48 AM10/15/13
to ratch...@googlegroups.com
Is your WebSocket on the same port as the website?

Seshachalam Malisetti

unread,
Oct 16, 2013, 12:22:41 AM10/16/13
to ratch...@googlegroups.com
No not on the same port. Websocket server is on 8080 and web app is on 80

cboden

unread,
Oct 27, 2013, 10:56:53 AM10/27/13
to ratch...@googlegroups.com
Web browsers treat different ports as different domains and therefore would not send session cookies. 

مهران نمیگم

unread,
Jan 30, 2015, 3:09:00 PM1/30/15
to ratch...@googlegroups.com
hi Kuba 
I saw a problem in your index.php code

you forget Session class parameter.
.
.
.

$memcache
= new Memcache;
$memcache
->connect('localhost', 11211);
$storage
= new NativeSessionStorage(array(), new MemcacheSessionHandler($memcache));



$session
= new Session($storage);#here you forget put $storage as parameter


$session
->start();
.
.
.

gustav...@gmail.com

unread,
Jul 20, 2016, 2:46:53 PM7/20/16
to Ratchet
Dear, I have exactly the same problem as you. The cookie OnOpen is an empty array. I would appreciate if you tell me as solved.
Reply all
Reply to author
Forward
0 new messages