从客户端中的服务器获取价值

时间:2018-07-18 19:09:01

标签: javascript node.js electron

我正在尝试从我的服务器在Electron / nodejs中返回一个字符串,并且遇到了一些麻烦。

我有一个名为handleHWCompare的函数,该函数具有发送给它的类型和值。对于某些类型,我需要将值发送到服务器,该服务器将处理并返回稍有变化的字符串以匹配脚本中的某些特定情况。

问题是,我不知道如何在函数中正确使用返回的数据。

此函数具有多种类型的返回值,因为它有多种用途。这是我的功能:

function handleHWCompare(type, server, client, doActions = true) {

    difference = leven(server, client);

    if(difference <= 2) {
        if(doActions) {
            //do some actions
        }
        return true;
    } else {
        if(doActions) {
            //do some actions
        }
        return false;
    }

}

我为此做了一些尝试。

在函数顶部,我尝试了此操作:

if(type == "mobo" || type == "sku") {
    https.get('https://example.com/api/check_rules.php?type=' + type + '&value='+ client, (res) => {
        res.setEncoding('utf8');
        res.on('data', function (result) {
            console.log('altered: ' + result);
            client = result;
        });
    });
    console.log('variable: ' + client);
}

我知道这是一个问题,因为如果同步/异步,但实际上并没有及时设置使用该值,它将保留为该函数的旧值。

我尝试将功能中所有现有的代码放入https.get的res.on部分中,但这没有用,因为它破坏了return truereturn false

我尝试使用几乎可以正常工作的回调函数,但无法弄清楚如何使现有的return truereturn false部分正常工作,因此它破坏了我的脚本(我没有我已经尝试了几个小时,所以代码不再存在。

做这么简单的事情似乎太困难了,我不想为此重写我的整个应用程序结构。如何在此函数内的客户端中发送该值并检索更改的版本?

我知道诸如等待和承诺之类的内容,但是无论我怎么努力,我都无法理解如何使用它们的概念。

4 个答案:

答案 0 :(得分:1)

希望这会有所帮助。

async function handleHWCompare(type, server, client, doActions = true) {
        if(type == "mobo" || type == "sku") {
          https.get('https://pccheck.r1ss.com/api/check_rules.php?type=' + type + '&value='+ client, (res) => {
                res.setEncoding('utf8');
                res.on('data', function (result) {
                    console.log(result);
                    return await handleHWCompareReal(type, server, result, doActions);
                });
            }).on('error', (e) => {
              console.error(e);
            });
        } else {
            return await handleHWCompareReal(type, server, client, doActions);
        }
}

function handleHWCompareReal(type, server, client, doActions = true) {
//place your code
}

答案 1 :(得分:0)

考虑到它在范围内,我想您只需要调用函数即可。 但是我需要更多有关代码结构的信息以确保。

if(type == "mobo" || type == "sku") {
  https.get('https://example.com/api/check_rules.php?type=' + type + '&value='+ client, (res) => {
    res.setEncoding('utf8');
    res.on('data', function (result) {
      console.log('altered: ' + result);
      handleHWCompare(type, server, result);
    });
  });
}

答案 2 :(得分:0)

一种可行的方法是创建一个getAlteredClient函数,该函数返回一个Promise,如下所示:

async function getAlteredClient(type, client) {
    return new Promise((resolve, reject) => {
        https.get('https://example.com/api/check_rules.php?type=' + type + '&value='+ client, (res) => {
            res.setEncoding('utf8');
            res.on('data', result => {
               resolve(result)
            });
        });
    });
}

您稍后可能要处理可能的HTTP错误,并利用reject函数

现在,您可以使handleHWCompare函数异步并使用getAlteredClient来使用await

async function handleHWCompare(type, server, client, doActions = true) {
    if(type == "mobo" || type == "sku") {
        client = await getAlteredClient(type, client)
    }

    difference = leven(server, client);

    if(difference <= 2) {
        if(doActions) {
            //do some actions
        }
        return true;
    } else {
        if(doActions) {
            //do some actions
        }
        return false;
    }

}

请注意,您可能必须使用handleHWCompareawait来更改对then()的呼叫的管理方式,因为它现在是异步功能。

答案 3 :(得分:0)

不太清楚您的意思。我想你想要这样的东西。

var genericMethod = ((Func<string, int>)Json.DeserializeObject<int>).Method.GetGenericMethodDefinition();
var boundMethod = genericMethod.MakeGenericMethod(SettingTypes[record.SettingCode]);
var result = boundMethod.Invoke(null, rec.TENS_Setting)...

然后使用

sudo ifconfig lo0 alias 10.254.254.254