如何从MQL4向NodeJS发送POST消息

How to send a POST message from MQL4 to NodeJS?

本文关键字:发送 POST 消息 NodeJS MQL4      更新时间:2023-09-26

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/'); });

请注意细节:

步骤0:
MQL4部分
应遵循最近的新建-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服务,由于上述原因,在web上搜索"帮助"将成为未经编辑的复制/粘贴尝试的误导来源。


步骤1:
POSThttp语法构造
应符合RFC 7231Section 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编码的http消息中传递的参数。

工作完成了。


欢迎来到MQL4

的狂野世界