(Solved) Websocket(WSS) is complicated

 

I'm not sure if MQL know their Socket TLS functions aren't suitable for Websocket, i've been spending all time of my every last 5 days searching this forum, that forum and all forum on how to make WSS Websocket work all to no avail.

Should one just go to WinAPI for every little function?

 
Racheal Samson:I'm not sure if MQL know their Socket TLS functions aren't suitable for Websocket, i've been spending all time of my every last 5 days searching this forum, that forum and all forum on how to make WSS Websocket work all to no avail. Should one just go to WinAPI for every little function?

Sockets and Websockets are two different things. You can however use MQL5 Sockets to write a websocket library of your own and it will work.

The existing limitation however, is that you have to grant web address access permission in the MetaTrader Options.

If however, I am misunderstanding your words, then please explain why you say that they are not suitable?

 
I didn't try myself, but some users on this site wrote articles or even sell products about WebSockets using MQL Socket. So I have some doubt about your affirmation, sorry.
 
Fernando Carreiro #:

Sockets and Websockets are two different things. You can however use MQL5 Sockets to write a websocket library of your own and it will work.

The existing limitation however, is that you have to grant web address access permission in the MetaTrader Options.

If however, I am misunderstanding your words, then please explain why you say that they are not suitable?

LOL, I'm not sure if being the smartest qualifies to making MQL Socket work with Websocket Upgrade from HTTP, i've tried all possible to make it work in pure MQL, nothing works; if you need reference to past tries(some since years back), i can get a lot for you.

It just doesn't work for WSS, only the WS.

 

You may be completely depending on this article which is not made to work with wss, As Alain said, there are already products available in marketplace which are working with WSS without any external DLL, it means you have to write your own code, everything is fine with MQL and WSS. I doubt your affirmation too.

 

I eventually got it working with WSS, but mehn MQL Socket is complicated if one has to go by common sense or the book.

 
Alain Verleyen #:
I didn't try myself, but some users on this site wrote articles or even sell products about WebSockets using MQL Socket. So I have some doubt about your affirmation, sorry.
After checking myself, I confirm there is NO problem to have WebSockets, through TLS or without, working with MQL Socket.
 
@Racheal I am looking to create some WSS connections. Please explain how did you get it working with WSS?
 
Alain Verleyen #:
After checking myself, I confirm there is NO problem to have WebSockets, through TLS or without, working with MQL Socket.Ther

There is no problem when connecting through http:// or https:// but not working through ws:// or wss://. I tried to use WebsocketClient.mqh and Socket.mqh libraries. There are only inputs url, port but not http or ws configuration.

 
Adiyakhuu Gantumur #:

There is no problem when connecting through http:// or https:// but not working through ws:// or wss://. I tried to use WebsocketClient.mqh and Socket.mqh libraries. There are only inputs url, port but not http or ws configuration.

Have also problems to establish a secured connection to websocket server with the inbuild CWebSocketClient.

Here is the expert code:

#include<Websocket/WebSocketClient.mqh>
CWebSocketClient wsc;

string MyUrl="wss://echo.websocket.org";//"wss://127.0.0.1";
int MyPort=443;//9000;
int    MaxSize=256;
bool UseTLS=true;
bool connected=false;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{

                
           wsc.SetMaxSendSize(MaxSize);
           if( (connected=wsc.Connect(MyUrl,MyPort,5000,UseTLS,true)))
           {
              int sent=wsc.SendString("it works");
              Print("sent data is "+IntegerToString(sent));           
           }
           else
                        Print("connection failed");

        EventSetMillisecondTimer(1000);
   return(INIT_SUCCEEDED);
}

This connection always fails in function CWebSocketClient::upgrade.

When i use a node.js script, connection to 

wss://echo.websocket.org

, then this works fine.

echo.websocket.org

is listed as webrequest address in MetaTrader. Otherwise, the connection would be blocked, which is not the case. So, i guess this is not the issue. it fails in ::upgrade at line:

if(StringFind(result,"Sec-WebSocket-Accept: "+sh1confirm)>=0))

Here is the node.js script that connects fine:

const WebSocket = require('ws');

// Create a WebSocket client
const ws = new WebSocket('wss://echo.websocket.org', {
  rejectUnauthorized: 
true 
});

// Event listener for when the connection is established
ws.on('open', function open() {
  console.log('Connected to the server');

  // Send a message to the server
  ws.send('hi server');
});

// Event listener for incoming messages from the server
ws.on('message', function incoming(data) {
  console.log('Received from server: %s', data);
});

// Event listener for errors
ws.on('error', function error(err) {
  console.error('Connection error: ', err);
});

// Event listener for when the connection is closed
ws.on('close', function close(code, reason) {
  console.log('Disconnected from the server');
  console.log('Code: ', code);
  console.log('Reason: ', reason);
});

Any help appreciated to get the CWebSocketClient client run in secure mode.

Thank you

 
MQL5 Book: Advanced language tools / Projects / WebSocket protocol in MQL5
MQL5 Book: Advanced language tools / Projects / WebSocket protocol in MQL5
  • www.mql5.com
We have previously looked at Theoretical foundations of the WebSockets protocol . The complete specification is quite extensive, and a detailed...