PHP Symfony 2在控制器之间传递包含套接字的类
我使用的是PHP5.5.9和Symfony 2.2.7, 我需要在控制器之间传递一个包含PHP套接字数组的类数组。 我遇到的问题是,我无法通过放置0而不是套接字的会话来传递它,当我尝试通过每次重置类时都要使用的服务传递它时,我尝试将数组置于静态状态,但这也不起作用 有没有人有线索、线索或什么可以帮我的 至少感谢你的阅读,祝你有一个愉快的一天 以下是将插座重新封装的类:PHP Symfony 2在控制器之间传递包含套接字的类,php,sockets,symfony,Php,Sockets,Symfony,我使用的是PHP5.5.9和Symfony 2.2.7, 我需要在控制器之间传递一个包含PHP套接字数组的类数组。 我遇到的问题是,我无法通过放置0而不是套接字的会话来传递它,当我尝试通过每次重置类时都要使用的服务传递它时,我尝试将数组置于静态状态,但这也不起作用 有没有人有线索、线索或什么可以帮我的 至少感谢你的阅读,祝你有一个愉快的一天 以下是将插座重新封装的类: class Socket { private $socket; private $give; private static $o
class Socket {
private $socket;
private $give;
private static $out;
function __construct($host, $port = -1){
$errno = 0;
$errmsg = "";
$this->give = false;
if ($port === -1) {
$this->socket = $host->getFd();
$this->give = true;
}
else {
$this->socket = fsockopen($host, $port, $errno, $errmsg);
echo "connection\n";
}
}
function __destruct() {
if (!$this->give && !$this->socket === false)
fclose($this->socket);
}
function getFd() {
return ($this->socket);
}
function writeSocket($buff, $size) {
if ($this->socket === false)
return (false);
echo "send: " . bin2hex($buff) . "\n";
return (fwrite($this->socket, $buff, $size));
}
function readSocket($size) {
if ($this->socket === false)
return (false);
$out = "";
$out = fgets($this->socket, $size + 1);
if (strlen($out) < $size) {
$tmp = fgets($this->socket, $size - strlen($out) + 1);
if (!($tmp === false))
$out .= $tmp;
}
echo "read: " . bin2hex($out) . "\n";
return ($out);
}
}
这里是带有数组的类:
class ConnexionsHandler {
private static $socket;
private $maxTime = 120;
function __construct(){
echo "construct";
if (!isset(ConnexionsHandler::$socket))
{
echo "is not set";
ConnexionsHandler::$socket = array();
}
print_r(ConnexionsHandler::$socket);
}
function __destruct(){
echo "destruct";
}
private function cleanConnexion(){
foreach (ConnexionsHandler::$socket as $key => $val) {
if (time() - $val[0] > $this->maxTime){
unset(ConnexionsHandler::$socket[$key]);
}
}
}
public function getConnexion($user){
$this->cleanConnexion();
if (array_key_exists($user, ConnexionsHandler::$socket)){
ConnexionsHandler::$socket[$user][0] = time();
return (ConnexionsHandler::$socket[$user][1]);
}
return (null);
}
public function createConnexion($new, $user, $pass){
$this->cleanConnexion();
$client = new EcoProtocol();
$ret = $client->logIn($new, $user, $pass);
ConnexionsHandler::$socket[$user][0] = time();
ConnexionsHandler::$socket[$user][1] = $client;
return ($ret);
}
}你能展示你的代码吗?套接字是一种资源,你知道,你不能在会话中序列化它们。这是我所理解的,我添加了类。
class ConnexionsHandler {
private static $socket;
private $maxTime = 120;
function __construct(){
echo "construct";
if (!isset(ConnexionsHandler::$socket))
{
echo "is not set";
ConnexionsHandler::$socket = array();
}
print_r(ConnexionsHandler::$socket);
}
function __destruct(){
echo "destruct";
}
private function cleanConnexion(){
foreach (ConnexionsHandler::$socket as $key => $val) {
if (time() - $val[0] > $this->maxTime){
unset(ConnexionsHandler::$socket[$key]);
}
}
}
public function getConnexion($user){
$this->cleanConnexion();
if (array_key_exists($user, ConnexionsHandler::$socket)){
ConnexionsHandler::$socket[$user][0] = time();
return (ConnexionsHandler::$socket[$user][1]);
}
return (null);
}
public function createConnexion($new, $user, $pass){
$this->cleanConnexion();
$client = new EcoProtocol();
$ret = $client->logIn($new, $user, $pass);
ConnexionsHandler::$socket[$user][0] = time();
ConnexionsHandler::$socket[$user][1] = $client;
return ($ret);
}