Native Websocket
- Bibliotecas
- Racheal Samson
- Versão: 1.395
- Atualizado: 29 setembro 2024
- Ativações: 15
An easy to use, fast, asynchronous WebSocket library for MQL5.
It supports:
- ws:// and wss:// (Secure "TLS" WebSocket)
- text and binary data
It handles:
- fragmented message automatically (large data transfer)
- ping-pong frames automatically (keep-alive handshake)
Benefits:
- No DLL required.
- No OpenSSL installation required.
- Up to 128 WebSocket Connections from a single program.
- Various Log Levels for error tracing
- Can be synchronized to MQL5 Virtual Hosting.
- Completely native to MQL5.
Sample code below:
//include WSMQL.mqh - a file that has all the declarations required to interact with the library #include <WSMQL.mqh> // Methods below // class CWebSocketClient { // public: // WSHANDLE Initialize(void); // void ZeroHandle(void); // ENUM_WEBSOCKET_STATE ClientState(WSHANDLE handle); // void SetMaxSendSize(WSHANDLE handle, int max_send_size); // void SetOnMessageHandler(OnWebsocketMessage callback); // void SetOnBinaryMessageHandler(OnWebsocketBinaryMessage callback); // bool Connect(const WSHANDLE handle, const string url, const uint port = 443, const uint timeout = 5000, bool use_tls = true, ENUM_LOG_LEVEL log_level = LOG_LEVEL_ERROR); // bool Disconnect(WSHANDLE handle, ENUM_CLOSE_CODE close_code = NORMAL_CLOSE, const string msg = ""); // int SendString(WSHANDLE handle, const string message); // int SendData(WSHANDLE handle, uchar& message_buffer[]); // int SendPong(WSHANDLE handle, const string msg = ""); // int SendPing(WSHANDLE handle, const string msg); // uint ReadString(WSHANDLE handle, string& out); // uint ReadBinary(WSHANDLE handle, uchar& out[]); // uint ReadStrings(WSHANDLE handle, string& out[]); // uint OnReceiveString(WSHANDLE handle); // uint OnReceiveBinary(WSHANDLE handle); // } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { // Create an instance of the Client CWebSocketClient client; // Check if the client is initialized if (!client.Initialized()) { ZeroHandle();//Cleanup all clients return; } // Set OnMessage handler client.SetOnMessageHandler(OnWMessage);// use OnReceiveBinary for binary messages // URL and msg declaration string url = "stream.binance.com/ws";//Or wss://stream.binance.com/ws string msg = "{\"params\":[\"btcusdt@bookTicker\"],\"method\":\"SUBSCRIBE\",\"id\":27175}"; //ALERT: Make sure stream.binance.com has been added to WebRequest list in Options -> Expert Advisors tab // Connect to the WebSocket server if (!client.Connect(url, 443)) { ZeroHandle();//Cleanup all clients return; } // Send a string message client.SendString(msg); // Process messages until the script is stopped while (true) { if (IsStopped()) break; // Receive string messages and process them using the OnMessage callback uint frames = client.OnReceiveString(); Print("Frames Processed : ", frames); } // Disconnect from the WebSocket server Print("Disconnecting..."); if (client.Disconnect()) { ZeroHandle();//Cleanup all clients Print("Disconnected!"); } } //+------------------------------------------------------------------+ void OnWMessage(string message) { Print(message); } //+------------------------------------------------------------------+ //Sample Outputs: //{"result":null,"id":27175} //Frames Processed : 1 //--- //{"u":35893555769,"s":"BTCUSDT","b":"27812.78000000","B":"7.14299000","a":"27812.79000000","A":"0.81665000"} //{"u":35893555770,"s":"BTCUSDT","b":"27812.78000000","B":"7.14299000","a":"27812.79000000","A":"0.82309000"} //{"u":35893555771,"s":"BTCUSDT","b":"27812.78000000","B":"7.14964000","a":"27812.79000000","A":"0.82309000"} //Frames Processed : 3
Feel free to contact me for support and questions before/after purchase.
Awesome support from Racheal, thanks for your help setting up the library, keep up the great work ;)