AzerothCore 有 4 种与世界服务器(world server)交互的方式。
游戏内菜单和控制台不言自明,但你并不总能本地管理服务器,而且前两种方式也没有提供太多自动化机会。如果你想提供远程访问服务器的方式,或让软件来管理它,那么你有两个选择。
远程访问基本上就是一个 telnet 连接,并且具有 telnet 连接带来的一切缺点。这些缺点包括:
然而,它也有优点,即:
远程访问非常适合在本地服务器上运行,并通过 SSH 安全地访问它来发送命令。简单就是它的优势。此外,它内置在大多数机器中,无需任何配置。
SOAP 是简单对象访问协议(Simple Object Access Protocol)的缩写,是一种在机器之间共享结构化数据的格式。它是 ReST 和 json 常见的 XML 对应物,可以让两段软件尽管运行在不同的代码库、语言和操作系统上,仍能相互交互。接下来看看它的缺点。
以及它的优点。
用更简化的说法:网站用 SOAP,命令行用 telnet。
account_access 表,并确保你的用户的 realmID 为 -1(表示所有服务器)。由于 telnet 无处不在,几乎在任何地方都很容易使用。
telnet localhost 3443SOAP 使用标准的 HTTP POST 工作。整个 POST 负载都是 XML。
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:ns1="urn:AC">
<SOAP-ENV:Body>
<ns1:executeCommand>
<command>server status</command>
</ns1:executeCommand>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
响应看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:ns1="urn:AC">
<SOAP-ENV:Body>
<ns1:executeCommandResponse>
<result>AzerothCore rev. 6f4f0043c2ab+ 2021-05-18 02:16:59 +0200 (master branch) (Win64, RelWithDebInfo)
Connected players: 0. Characters in world: 0.
Connection peak: 0.
Server uptime: 5 second(s).
Update time diff: 10ms, average: 10ms.
</result>
</ns1:executeCommandResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
错误响应看起来像这样
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:ns1="urn:AC">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring>Error 401: HTTP 401 Unauthorized</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
你需要通过将用户名和密码放入 URI 来进行身份验证,如下所示:http://soapuser:abcd1234@localhost:7878/(这也称为"basic auth")
目前,设置请求头 Content-Type: application/xml 并不是必需的。
使用内置的 SoapClient
$conn = new SoapClient(NULL, array(
'location' => "http://:/",
'uri' => 'urn:AC',
'style' => SOAP_RPC,
'login' => 'soapuser',
'password' => 'abcd1234'
));
echo $conn->executeCommand(new SoapParam('server info', 'command'));
使用 xml2js 解析响应。请务必对输入进行消毒处理。
function AzerothCore_Soap(command){
return new Promise((resolve, reject)=>{
const req = http.request({
port: 7878,
method: "POST",
hostname: "localhost",
auth: "soapuser:abcd1234",
headers: { 'Content-Type': 'application/xml' }
}, res=>{
res.on('data', async d => {
const xml = await xml2js.parseStringPromise(d.toString());
const body = xml["SOAP-ENV:Envelope"]["SOAP-ENV:Body"][0];
const fault = body["SOAP-ENV:Fault"];
if(fault){
resolve({
faultCode : fault[0]["faultcode"][0],
faultString: fault[0]["faultstring"][0],
});
return;
}
const response = body["ns1:executeCommandResponse"];
if(response){
resolve({
result: response[0]["result"][0]
});
return;
}
console.log(d.toString());
})
});
req.write(
'<SOAP-ENV:Envelope' +
' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"' +
' xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"' +
' xmlns:xsd="http://www.w3.org/1999/XMLSchema"' +
' xmlns:ns1="urn:AC">' +
'<SOAP-ENV:Body>' +
'<ns1:executeCommand>' +
'<command>'+command+'</command>' +
'</ns1:executeCommand>' +
'</SOAP-ENV:Body>' +
'</SOAP-ENV:Envelope>'
);
req.end();
});
}