<?php
require __DIR__.'/vendor/php/autoload.php';
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler;
$redis = new Redis();
$redis->connect('127.0.0.1:6379');
$storage = new NativeSessionStorage(array(), new RedisSessionHandler($redis,array(),"PHPREDIS_SESSION"));
$session = new Session($storage);
$session->start();
$session->set( "name", "Lucas" );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document</title>
<script>
var conn = new WebSocket('ws://www.nstock.com.ar:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
</script>
</head>
<?php
var_dump( $_SESSION );
var_dump( $session->all() );
?>
<body>
</body>
</html>
array(3) {
["_sf2_attributes"]=>
&array(1) {
["name"]=>
string(5) "Lucas"
}
["_sf2_flashes"]=>
&array(0) {
}
["_sf2_meta"]=>
&array(3) {
["u"]=>
int(1422985597)
["c"]=>
int(1422977071)
["l"]=>
string(1) "0"
}
}
array(1) {
["name"]=>
string(5) "Lucas"
}
root@ubuntu:/usr/share/nginx/html# redis-cli
127.0.0.1:6379> KEYS *
1) "PHPREDIS_SESSION:lvb0bqcen2ngmkf77pfmmqsui6"
127.0.0.1:6379> get PHPREDIS_SESSION:lvb0bqcen2ngmkf77pfmmqsui6
"\x84\xc0\x01\xaf_sf2_attributes\x82\xc0\x01\xa4name\xa5Lucas\xac_sf2_flashes\x90\xa9_sf2_meta\x84\xc0\x01\xa1u\xceT\xd1\t}\xa1c\xceT\xd0\xe8/\xa1l\xa10"
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class MyApp implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
var_dump( $conn->WebSocket->request->getCookies() );
var_dump( $conn->Session );
$conn->send( "Hello!" );
}
public function onMessage(ConnectionInterface $from, $msg) {
}
public function onClose(ConnectionInterface $conn) {
}
public function onError(ConnectionInterface $conn, \Exception $e) {
}
}
?>
array(1) {
["PHPSESSID"]=>
string(26) "lvb0bqcen2ngmkf77pfmmqsui6"
}
object(Symfony\Component\HttpFoundation\Session\Session)#76 (3) {
["storage":protected]=>
object(Ratchet\Session\Storage\VirtualSessionStorage)#77 (6) {
["_serializer":protected]=>
object(Ratchet\Session\Serialize\PhpHandler)#9 (0) {
}
["bags":protected]=>
array(2) {
["attributes"]=>
object(Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag)#80 (3) {
["name":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=>
string(10) "attributes"
["storageKey":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag":private]=>
string(15) "_sf2_attributes"
["attributes":protected]=>
array(0) {
}
}
["flashes"]=>
object(Symfony\Component\HttpFoundation\Session\Flash\FlashBag)#81 (3) {
["name":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=>
string(7) "flashes"
["flashes":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=>
array(0) {
}
["storageKey":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":private]=>
string(12) "_sf2_flashes"
}
}
["started":protected]=>
bool(false)
["closed":protected]=>
bool(false)
["saveHandler":protected]=>
object(Ratchet\Session\Storage\Proxy\VirtualProxy)#78 (6) {
["_sessionId":protected]=>
string(26) "lvb0bqcen2ngmkf77pfmmqsui6"
["_sessionName":protected]=>
string(9) "PHPSESSID"
["handler":protected]=>
object(Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler)#7 (8) {
["redis":protected]=>
object(Redis)#2 (1) {
["socket"]=>
resource(11) of type (Redis Socket Buffer)
}
["ttl":protected]=>
int(0)
["prefix":protected]=>
string(16) "PHPREDIS_SESSION"
["locking":"Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler":private]=>
bool(true)
["locked":"Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler":private]=>
bool(false)
["lockKey":"Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler":private]=>
NULL
["spinLockWait":"Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler":private]=>
int(150000)
["lockMaxWait":"Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler":private]=>
int(30)
}
["wrapper":protected]=>
bool(false)
["active":protected]=>
bool(false)
["saveHandlerName":protected]=>
string(4) "user"
}
["metadataBag":protected]=>
object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)#79 (5) {
["name":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
string(10) "__metadata"
["storageKey":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
string(9) "_sf2_meta"
["meta":protected]=>
array(3) {
["c"]=>
int(0)
["u"]=>
int(0)
["l"]=>
int(0)
}
["lastUsed":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
NULL
["updateThreshold":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":private]=>
int(0)
}
}
["flashName":"Symfony\Component\HttpFoundation\Session\Session":private]=>
string(7) "flashes"
["attributeName":"Symfony\Component\HttpFoundation\Session\Session":private]=>
string(10) "attributes"
}
<?php
require __DIR__.'/vendor/php/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Session\SessionProvider;
use Snc\RedisBundle\Session\Storage\Handler\RedisSessionHandler;
use Ratchet\App;
$redis = new Redis();
$redis->connect('127.0.0.1:6379');
$server = IoServer::factory(
new HttpServer(
new WsServer(
new SessionProvider(
new MyApp,
new RedisSessionHandler($redis,array(),"PHPREDIS_SESSION")
)
)
),
8080
);
$server->run();
?>
...
<?php
require __DIR__.'/vendor/php/autoload.php';
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->start();
$session->set( "name", "Lucas" );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document</title>
<script>
var conn = new WebSocket('ws://www.nstock.com.ar:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
</script>
</head>
<?php
var_dump( $_SESSION );
var_dump( $session->all() );
?>
<body>
</body>
</html>
<?php
namespace Ratchet\Session\Serialize;
class MsgpackHandler implements HandlerInterface {
function serialize(array $data) {
throw new \RuntimeException("Serialize PhpHandler:serialize code not written yet, write me!");
}
public function unserialize($raw) {
return msgpack_unpack($raw);
}
}
?>
...