【发布时间】:2014-02-06 02:48:03
【问题描述】:
我已经到处搜索了这个并且我遇到了很多问题,但我不认为这是实际代码的问题。 基本上这段代码在两个单独的线程中启动套接字服务器(登录和游戏),我基本上从非线程版本转换了这段代码,但我一直无法让它为线程工作。
include "socket.php";
include "dep.php";
class Server extends Thread {
public $server;
public $config;
public function __construct($type){
//$this->config = (string)$type;
$this->run2($type);
$this->run();
}
public function run(){
while(true){
$this->server->loop();
}
}
public function run2($config){
$this->server = new sokserv($config);
$this->server->init();
//while(true){
// $this->server->loop();
//}
}
}
$login = new Server('Config/config.xml');
$game = new Server("Config/config2.xml");
The error received is
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in C:\Users\=\Desktop\Test\Start.php:19
Stack trace:
#0 C:\Users\=\Desktop\Test\Start.php(19): Server->run2()
#1 C:\Users\=\Desktop\Test\Start.php(10): Server->run2('Config/config.x...')
#2 C:\Users\=\Desktop\Test\Start.php(26): Server->__construct('Config/config.x...')
#3 {main}
thrown in C:\Users\=\Desktop\Test\Start.php on line 19
但是,恢复到旧代码可以正常工作。 sokserv 的第一部分(我删除了一些公共变量,因为它们包含个人信息)
class sokserv
{
public $ip;
public $port;
public $users = array();
public $config;
public $mysql;
private $socket;
public function __construct($config = "Config/config.xml")
{
$this->readConfig($config);
}
public function readConfig($file)
{
if (!file_exists($file))
die("Could not find config");
$this->config = simplexml_load_file($file);
}
如果你想要这是 xml:
<server>
<port>6112</port>
<ip>0</ip>
<mysql>
<host>127.0.0.1</host>
<username>root</username>
<dbname></dbname>
<password></password>
<table></table>
</mysql>
</server>
【问题讨论】:
-
请分享 sockserv 实现或源代码链接。我认为您可能需要将配置变量转换为数组。
-
我无法分享 sokserv 类,因为它太大并且包含个人信息,但是我可以分享涉及配置的前几位。
-
是的,请这样做,您可以只共享一个构造函数
-
我已经编辑了问题,我还添加了 xml。
-
对不起,我没有注意到它与 Start.ph 文件有关。请看这个stackoverflow.com/questions/14912551/…
标签: php xml multithreading pthreads simplexml