Отправить ответ в MetaTrader на TCP-сервере Node.js - PullRequest
0 голосов
/ 08 марта 2019

В MetaTrader 5 я записываю данные в сокет наподобие Пример документа MQL5 в советниках, а данные отправляются на TCP-сервер Node.js в виде чудо-кнопки.Но MetaTrader не может получить ответ.Я не знаю что не так.Если кто-то может дать совет по этому вопросу.

Код MQL5-эксперта:

//+------------------------------------------------------------------+ 
//|                                                SocketExample.mq5 | 
//|                        Copyright 2018, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright   "Copyright 2018, MetaQuotes Software Corp." 
#property link        "https://www.mql5.com" 
#property version     "1.00" 
#property description "Add Address to the list of allowed ones in the terminal settings to let the example work" 
#property script_show_inputs 

input string Address="127.0.0.1"; 
input int    Port   =3100; 
bool         ExtTLS =false; 
//+------------------------------------------------------------------+ 
//| Send command to the server                                       | 
//+------------------------------------------------------------------+ 
bool HTTPSend(int socket,string request) 
  { 
   char req[]; 
   int  len=StringToCharArray(request,req)-1; 
   if(len<0) 
      return(false); 
//--- if secure TLS connection is used via the port 443 
   if(ExtTLS) 
      return(SocketTlsSend(socket,req,len)==len); 
//--- if standard TCP connection is used 
   return(SocketSend(socket,req,len)==len); 
  } 
//+------------------------------------------------------------------+ 
//| Read server response                                             | 
//+------------------------------------------------------------------+ 
bool HTTPRecv(int socket,uint timeout) 
  { 
   char   rsp[]; 
   string result; 
   uint   timeout_check=GetTickCount()+timeout; 
//--- read data from sockets till they are still present but not longer than timeout 
   do 
     { 
      uint len=SocketIsReadable(socket); 
      if(len) 
        { 
         int rsp_len; 
         //--- various reading commands depending on whether the connection is secure or not 
         if(ExtTLS) 
            rsp_len=SocketTlsRead(socket,rsp,len); 
         else 
            rsp_len=SocketRead(socket,rsp,len,timeout); 
         //--- analyze the response 
         if(rsp_len>0) 
           { 
            result+=CharArrayToString(rsp,0,rsp_len); 
            //--- print only the response header 
            int header_end=StringFind(result,"\r\n\r\n"); 
            if(header_end>0) 
              { 
               Print("HTTP answer header received:"); 
               Print(StringSubstr(result,0,header_end)); 
               return(true); 
              } 
           } 
        } 
     } 
   while(GetTickCount()<timeout_check && !IsStopped()); 
   return(false); 
  } 
//+------------------------------------------------------------------+ 
//| Script program start function                                    | 
//+------------------------------------------------------------------+ 
void OnTick() 
  { 
    double closePrice = iClose(Symbol(), Period(), 0);
    string priceStr = DoubleToString(closePrice);

   int socket=SocketCreate(); 
//--- check the handle 
   if(socket!=INVALID_HANDLE) 
     { 
      //--- connect if all is well 
      if(SocketConnect(socket,Address,Port,1000)) 
        { 
         Print("Established connection to ",Address,":",Port); 

         string   subject,issuer,serial,thumbprint; 
         datetime expiration; 
         //--- if connection is secured by the certificate, display its data 
         if(SocketTlsCertificate(socket,subject,issuer,serial,thumbprint,expiration)) 
           { 
            Print("TLS certificate:"); 
            Print("   Owner:  ",subject); 
            Print("   Issuer:  ",issuer); 
            Print("   Number:     ",serial); 
            Print("   Print: ",thumbprint); 
            Print("   Expiration: ",expiration); 
            ExtTLS=true; 
           } 
         //--- send GET request to the server 
         if(HTTPSend(socket, priceStr)) 
           { 
            Print("GET request sent"); 
            //--- read the response 
            if(!HTTPRecv(socket,1000)) 
               Print("Failed to get a response, error ",GetLastError()); 
           } 
         else 
            Print("Failed to send GET request, error ",GetLastError()); 
        } 
      else 
        { 
         Print("Connection to ",Address,":",Port," failed, error ",GetLastError()); 
        } 
      //--- close a socket after using 
      SocketClose(socket); 
     } 
   else 
      Print("Failed to create a socket, error ",GetLastError()); 
  } 
//+------------------------------------------------------------------+

Этот эксперт содержит два метода для отправки и получения данных через сокет.HTTPSend Запись данных в сокет и HTTPRecv Считывание ответа сервера и получение данных из сокета, но этот метод возвращает false, и это сообщение Не удалось получить ответ, ошибка 5275.

Сервер Node.jsкод:

const net = require('net');
const port = 3100;
const host = '127.0.0.1';

const server = net.createServer();
server.listen(port, host, () => {
    console.log('TCP Server is running on port ' + port + '.');
});

let sockets = [];

server.on('connection', function(sock) {
    console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
    sockets.push(sock);

    sock.on('data', function(data) {
        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        // Write the data back to all the connected, the client will receive it as data from the server
        sockets.forEach(function(sock, index, array) {
            sock.write(sock.remoteAddress + ':' + sock.remotePort + " said " + data + '\n');
        });
    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        let index = sockets.findIndex(function(o) {
            return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
        })
        if (index !== -1) sockets.splice(index, 1);
        console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
    });
});

1 Ответ

1 голос
/ 22 апреля 2019

Ошибка 5275: «Нет данных о сертификате, защищающем соединение». См. Документация mql5 . Кажется, код клиента пытается использовать защищенное соединение, а ваш сервер - нет?Вам нужно отладить код, чтобы проверить, что там происходит.

В любом случае, вы скопировали код из документации, но вам нужно будет настроить его, в основном HTTPRecv, чтобы правильно анализировать ответ с вашего сервера Node.js.,Этот код, как есть, будет печатать только заголовки HTTP из ответа в журнал.

Однако мне кажется, что вы пытаетесь использовать протокол HTTP на стороне клиента, в то время как ваш сервер использует только TCP?К сожалению, у меня нет опыта работы с Node.js, поэтому я не могу помочь в этом.

...