Native Websocket
- Kütüphaneler
- Racheal Samson
- Sürüm: 1.417
- Güncellendi: 1 Ekim 2024
- Etkinleştirmeler: 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: // bool Initialized(void); // Checks if the WebSocket client is initialized. // ENUM_WEBSOCKET_STATE State(void); // Returns the current state of the WebSocket connection. // void SetMaxSendSize(int max_send_size); // Sets the maximum send size for WebSocket messages. // bool SetOnMessageHandler(OnWebsocketMessage callback); // Sets the callback function for handling incoming text messages. // bool SetOnPingHandler(OnWebsocketMessage callback); // Sets the callback function for handling incoming ping messages. // bool SetOnPongHandler(OnWebsocketMessage callback); // Sets the callback function for handling incoming pong messages. // bool SetOnCloseHandler(OnWebsocketMessage callback); // Sets the callback function for handling WebSocket connection closures. // bool SetOnBinaryMessageHandler(OnWebsocketBinaryMessage callback); // Sets the callback function for handling incoming binary messages. // bool Connect(const string url, const uint port = 443, const uint timeout = 5000, bool use_tls = true, ENUM_LOG_LEVEL log_level = LOG_LEVEL_NONE); // Connects to a WebSocket server. // bool ConnectUnsecured(const string url, const uint port = 80, const uint timeout = 5000, ENUM_LOG_LEVEL log_level = LOG_LEVEL_NONE); // Connects to a WebSocket server using an unsecured connection. // bool ConnectSecured(const string url, const uint port = 443, const uint timeout = 5000, ENUM_LOG_LEVEL log_level = LOG_LEVEL_NONE); // Connects to a WebSocket server using a secured connection. // bool Disconnect(ENUM_CLOSE_CODE close_code = NORMAL_CLOSE, const string msg = ""); // Disconnects from the WebSocket server. // int SendString(const string message); // Sends a string message to the WebSocket server. // int SendData(uchar& message_buffer[]); // Sends binary data to the WebSocket server. // int SendPong(const string msg = ""); // Sends a pong message to the WebSocket server. // int SendPing(const string msg); // Sends a ping message to the WebSocket server. // uint ReadString(string& out); // Reads a string message from the WebSocket server. // uint ReadStrings(string& out[]); // Reads multiple string messages from the WebSocket server. // uint OnReceiveString(); // Receives and processes incoming string messages. // uint OnReceiveBinary(); // Receives and processes incoming binary messages. // uint OnMessage(); // Receives and processes incoming WebSocket messages. // }; // Create an instance of the Client CWebSocketClient client;//I declared this globally because of OnPingMessage requiring it //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { // Check if the client is initialized if (!client.Initialized()) { ZeroHandle();//Cleanup all clients return; } // Set OnMessage handler to receive text messages client.SetOnMessageHandler(OnMessage);// use SetOnBinaryMessageHandler for binary messages // Set OnPing handler to receive ping messages, // Pong will be automatically sent if this handler is not set client.SetOnPingHandler(OnPingMessage);// use SetOnPongHandler for pong 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 through either of the below-listed functions as required */ // Fully Configurable // if (!client.Connect(url/* , 80 || 443, 5000, false || true, LOG_LEVEL_INFO */)) { // For Non-TLS(unsecured) connection - without SSL required // if (!client.ConnectUnsecured(url/* , 80, 5000, LOG_LEVEL_INFO */)) { // For TLS(secured) connection - with SSL required if (!client.ConnectSecured(url/* , 443, 5000, LOG_LEVEL_INFO */)) { ZeroHandle();//Cleanup all clients return; } // Send a string message client.SendString(msg); // Process messages until the script is stopped while (true) { if (IsStopped()) break; if (client.State() == CLOSED) { Print("Socket connection closed"); //Reconnect? //client.ConnectSecured(url/* , 443, 5000, LOG_LEVEL_INFO */) //Or break the loop? break; } /* NB: You only need one of these functions */ // Receive all messages and process them using their respective On{Message | BinaryMessage | Ping | Pong | Close} callback(handler) uint frames = client.OnMessages(); // Receive messages and process only TEXT frames using the OnMessage callback // uint frames = client.OnStringMessages(); // Receive messages and process only BINARY frames using the OnBinaryMessage callback // uint frames = client.OnBinaryMessages(); if (frames > 0) Print("Frames Processed : ", frames); } // Disconnect from the WebSocket server Print("Disconnecting..."); if (client.Disconnect()) { Print("Disconnected!"); } else { Print("Failed to disconnect!"); } //Cleanup all clients ZeroHandle(); } //+------------------------------------------------------------------+ void OnMessage(string message) { Print(message); } //+------------------------------------------------------------------+ void OnPingMessage(string message) { Print("ping received:", message); if (client.SendPong() > 0) { Print("Pong sent successfully."); } else { Print("Failed to send pong."); } } //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 //--- //Frames Processed : 1 //ping received: ping //Pong sent successfully.
Feel free to contact me for support and questions before/after purchase.
This, Native WebSocket library by Racheal Samson is fast, handles secure wss:// connections effortlessly, and can manage large data transfers with ease.
I love that it's fully native to MQL5, with no extra installations required.
What really stood out was the author's quick response and genuine willingness to help, making the experience even smoother. Highly recommend both the library and the excellent support behind it!