【问题标题】:How to send a POST message from MQL4 to NodeJS?如何从 MQL4 向 NodeJS 发送 POST 消息?
【发布时间】:2016-04-17 02:47:53
【问题描述】:

webrequest.mq4

#property copyright "Copyright 2013, apla"
#property link      "-"

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----    
// WebRequest
   string cookie = NULL;
   string headers = "Content-Type: application/x-www-form-urlencoded";
   int res;

   string url = "localhost:8080";                              // url = localhost:8080
   char post[], result[];
   string signal = "account=" + AccountNumber() + "&balance=" + AccountBalance() + "&equity=" + AccountEquity(); 
   StringToCharArray( signal, post );
   Print( signal );
   int timeout = 5000;                                   // 5 sec
   res = WebRequest( "POST",
                     url,
                     cookie,
                     NULL,
                     timeout,
                     post,
                     ArraySize( post ),
                     result,
                     headers
                     );

   Print( "Status code: " , res, ", error: ", GetLastError() );
//----
   return(0);
}
//+------------------------------------------------------------------+  

我想将文件从 MetaTrader 终端 4 webrequest.mq4 发送到本网站部分的节点,但是可以放弃。

MT4 >> Nodejs

???邮政[] ??? (JavaScript 节点) 账户、余额、权益

如何将 file.php 转换为 nodejs

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "account ".$_POST['account']."\n";
fwrite($myfile, $txt);
$txt = "balance ".$_POST['balance']."\n";
fwrite($myfile, $txt);
$txt = "equity ".$_POST['equity']."\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

我不知道如何获得 POST。

writeFile.js

var http = require('http'); var fs = require('fs');

fs.writeFile("file.txt",??? POST[] ???, function(err,data) {
     if (err) throw err; 
     console.log('The file was saved!');

     http.createServer(function(req, res) {
          res.writeHead(200, {'Content-Type': 'text/plain'});
          res.end('OK'); 
}).listen(8080); 
console.log('Server running at http://localhost:8080/'); });

【问题讨论】:

  • 请清理问题的格式。
  • 好的,我清理格式

标签: javascript php node.js mql4


【解决方案1】:

请注意细节:

第 0 步:
MQL4 部分
应该按照最近的New-MQL4.56789复制/粘贴代码失败

作为第一个迹象,MetaTrader Terminal 4 静态存在于网络上的代码库并不反映 MQL4 语言的语法变化。最近MQL4 更接近MQL5(原因不在本帖内,如果有兴趣,请查看关于New-MQL4.56789 的其他帖子。

int start(){...}             // cannot be used anymore,
                             //           neither for EXPERT_ADVISOR
                             //           nor     for SCRIPT

最近的#property strict编译模式强制使用:

 void OnTick(){         ...} // for EXPERT_ADVISOR   type of MQL4-code
 void OnStart(){        ...} // for SCRIPT           type of MQL4-code
 int  OnCalculate(...){ ...} // for CUSTOM_INDICATOR type of MQL4-code,
                             // while,
                             //     CUSTOM_INDICATOR has explicitly
                             //     FORBIDDEN any attempt
                             //     call to a WebRequest( ... ) et al

也就是说,您的 MQL4-部分代码应在其主要结构中进行修改,以反映这些事实。

对于与 MQL4 相关的任何其他任务,请使用 IDE 中的 localhost 安装 Help-服务>,由于上述原因,在网络上搜索 “帮助” 将最容易成为未经编辑的复制/粘贴尝试的误导来源。


第 1 步:
POST http-syntax 构造
应符合 RFC 7231, Section 4.3.3

至少,您构造的文本存储在 string signal 中应该如下所示:

User-Agent: aplaHTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 54

account=123456789&balance=1234567.89&equity=1234567.89

第 2 步:
Node.js 部分
解析接收到的参数以进行任何进一步的后处理

同样,node.js 部分应破译 POST-url-encoded http-message 中传递的参数。

任务完成了。


欢迎来到MQL4狂野世界

【讨论】:

    猜你喜欢
    • 2014-08-28
    • 1970-01-01
    • 2020-10-11
    • 2021-03-07
    • 2012-07-03
    • 1970-01-01
    • 2023-04-01
    • 2015-07-31
    • 2019-11-06
    相关资源
    最近更新 更多