我需要知道在每個回呼函式結束后如何執行代碼行。
我正在使用rcon包向托管 Counter Strike 1.6 服務器的半條命專用服務器 (HLDS) 發送 rcon 命令并獲取回應。問題是有時回應被分成幾部分(我認為這是由于與 UDP 的連接,TCP 不起作用)。首先,我在 conn.on() 中使用 res.send() 和事件“回應”回呼函式,但我收到錯誤訊息說無法發送 HTTP 標頭。我了解了 res.write(),它可以保存我需要的內容,然后再使用 res.send()。但是我不知道在所有“回應”都回來之后我可以使用 res.send() 的正確位置在哪里。我嘗試在 conn.connect() 和其他地方之后使用該命令,但它沒有奏效。
const express = require('express');
var Rcon = require('rcon');
const app = express();
const port = 3000;
app.use(express.urlencoded({extended: true}));
app.get("/", function(req,res){
res.sendFile(__dirname "/index.html");
});
app.post("/", function(req, res) {
var resp = "";
var ip = req.body.serverip;
var pass = req.body.pass;
var command = req.body.command;
var options = {
tcp: false,
challenge: true
};
var conn = new Rcon(ip, 27015, pass, options);
conn.on('auth', function () {
// You must wait until this event is fired before sending any commands,
// otherwise those commands will fail.
console.log("Authenticated");
console.log("Sending command:" command);
conn.send(command);
});
res.setHeader("Content-Type", "text/html; charset=UTF-8");
res.write("<!DOCTYPE html><html><body>")
conn.on('response', function(str) {
console.log("Response: " str);
res.write(str);
});
conn.on('error', function (err) {
console.log("Error: " err);
});
conn.on('end', function () {
console.log("Connection closed");
process.exit();
});
conn.connect();
res.end("Done</body></html>");
});
app.listen(port, function(){
console.log("Server is listening on port " port);
});
});
uj5u.com熱心網友回復:
如果要將回應部分“流式傳輸”到 HTML 頁面,可以使用以下命令:
res.setHeader("Content-Type", "text/html; charset=UTF-8");
res.write("<!DOCTYPE html><html><body>"); // start an HTML page
conn.on('response', function(str) {
res.write(str);
});
您甚至在第一個回應到來之前發出的 HTML 頁面的基本開始,會導致瀏覽器在您將它們寫入頁面時顯示傳入回應的文本。(另見此處。)
如果您可以識別最后的回應,您可以完成頁面
conn.on('response', function(str) {
res.write(str);
if (str is the last response)
res.end("</body></html>");
});
但是即使沒有“最后”回應,“回應流”也可以作業:在這種情況下,瀏覽器會繼續等待更多,因此頁面永遠不會完成,但您仍然可以閱讀到目前為止的內容。
uj5u.com熱心網友回復:
看了這個答案讓我明白了。我只需要從 auth 中洗掉 conn.send() 并在 conn.connect() 之后超時使用它。與回應相同,超時然后使用 res.end();
app.post("/", function(req, res) {
var ip = req.body.serverip;
var pass = req.body.pass;
var command = req.body.command;
var options = {
tcp: false,
challenge: true
};
var conn = new Rcon(ip, 27015, pass, options);
conn.on('auth', function () {
// You must wait until this event is fired before sending any commands,
// otherwise those commands will fail.
console.log("Authenticated");
console.log("Sending command:" command);
// conn.send(command);
});
conn.on('response', function(str) {
console.log("Response: " str);
res.write("Response: " str);
});
conn.on('error', function (err) {
console.log("Error: " err);
res.sendFile(__dirname "/failure.html");
});
conn.on('end', function () {
// res.end("Done</body></html>");
console.log("Connection closed");
process.exit();
});
res.setHeader("Content-Type", "text/html; charset=UTF-8");
res.write("<!DOCTYPE html><html><body><pre>")
conn.connect();
setTimeout(function(){
conn.send(command);
},1000);
setTimeout(function(){
res.end("Done</pre></body></html>");
}, 2000);
});
uj5u.com熱心網友回復:
node 中有一個模塊可以將回呼轉換為 Promise 以促進這些型別的 fn
https://nodejs.org/dist/latest-v8.x/docs/api/util.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524488.html
