Native Websocket
- 라이브러리
- Racheal Samson
- 버전: 1.417
- 업데이트됨: 1 10월 2024
- 활성화: 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.
Download latest WSMQL.mqh from this link . Please ensure the MetaTrader downloaded library is downloaded/named as Native Websocket.ex5
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(OnWMessage);// use SetOnBinaryMessageHandler for binary messages // Set OnPing handler to receive ping messages, Pong will be automatically sent in the absence of this handler 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 // if (!client.Connect(url/* , 80, 5000, false, LOG_LEVEL_INFO */)) { // Fully Configurable // if (!client.ConnectUnsecured(url/* , 80, 5000, LOG_LEVEL_INFO */)) { // For Non-TLS(unsecured) connection 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; // Receive all messages and process them using their respective On{Message | BinaryMessage | Ping | Pong | Close} callback(handler) uint frames = client.OnMessage(); // 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); } //+------------------------------------------------------------------+ 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.
Awesome support from Racheal, thanks for your help setting up the library, keep up the great work ;)