Using Network Functions (Socket) to subscribe a PUB/SUB service

 

Hello Guys,

I had success using the new Network Function Socket to connect in a WebSocket service.

See that I receive a ping message every 3 seconds.


My question is: How I can send a new message to subscribe a channel, the message is:

I tried SocketSend but the WebSocket server not receive the message.

thanks

 
Andre Lima:

Hello Guys,

I had success using the new Network FunctionSocket to connect in a WebSocket service.

See that I receive a ping message every 3 seconds.


My question is: How I can send a new message to subscribe a channel, the message is:

I tried SocketSend but the WebSocket server not receive the message.

thanks

WebSocket is a different beast waken up by HTTP request.
You need more than simple HTTP request to handle WebSocket.

 
Soewono Effendi:

WebSocket is a different beast waken up by HTTP request.
You need more than simple HTTP request to handle WebSocket.

Handle websocket is not the problem, see image.. I can connect to the websocket and listen all messages ( See ping messages ).

The problem is that I can't send message. When I send a message with SocketSend, the websocket server not receive this message, but the return of SocketSend is that all right.

Thanks


Andre Lima

 
Working with sockets in MQL, or How to become a signal provider
Working with sockets in MQL, or How to become a signal provider
  • www.mql5.com
Sockets… What in our IT world could possibly exist without them? Dating back to 1982, and hardly changed up to the present time, they smoothly work for us every second. This is the foundation of network, the nerve endings of the Matrix we all live in. In the morning, you turn on the MetaTrader terminal, and it immediately creates sockets and...
 

Thanks Marco but my intention is use native MQL functions like the new Network Functions.

 
Andre Lima:

Handle websocket is not the problem, see image.. I can connect to the websocket and listen all messages ( See ping messages ).

The problem is that I can't send message. When I send a message with SocketSend, the websocket server not receive this message, but the return of SocketSend is that all right.

Thanks


Andre Lima

Show your code if you need coding help.

What WebSocket server are you using ?

 

Right @Alain Verleyen

Here is the code ( I simplified to read easy).

See Line 59 - After connect with the websocket server, I try send a new message and this message the websocket server not receive.

after run this script, if you go to http://ruby-websockets-chat.herokuapp.com/ and add a new message the socket will receive this message and print. 

I understand if the MT received this message, is a signal that is connected with the websocket.


//+------------------------------------------------------------------+
//|                                                    websocket.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
input string Address="ruby-websockets-chat.herokuapp.com";
input int    Port   =80;
//+------------------------------------------------------------------+ 
//| Send command to the server                                       | 
//+------------------------------------------------------------------+ 
bool HTTPSend(int socket,string request)
  {
   char req[];
   int  len=StringToCharArray(request,req)-1;
   if(len<0)
      return(false);
   return(SocketSend(socket,req,len)==len);
  }
//+------------------------------------------------------------------+ 
//| Read server response                                             | 
//+------------------------------------------------------------------+ 
void HTTPRecv(int socket)
  {
   char   rsp[];
   string result;
   int rsp_len;

   uint len=SocketIsReadable(socket);

   if(len)
     {
      rsp_len=SocketRead(socket,rsp,len,1000);
      result=CharArrayToString(rsp,0,rsp_len);
      Print("HTTP answer header received:");
      Print(result);
     }
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int socket=SocketCreate();
   //--- check the handle 
   if(socket!=INVALID_HANDLE)
     {
      if(SocketConnect(socket,Address,Port,1000))
        {
         Print("Established connection to ",Address,":",Port);
         //--- send GET request to the server 
         HTTPSend(socket,"GET / HTTP/1.1\r\nHost: ruby-websockets-chat.herokuapp.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n");
         //-- try send a new message - Here is the problem ( this message the websocket server not receive )
         HTTPSend(socket,"{\"handle\":\"MT5\",\"text\":\"Hello\"}\r\n");
         //--read the websocket 
         do
            HTTPRecv(socket);
         while(!IsStopped());
        }
      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());
  }
//+------------------------------------------------------------------+
Reason: