Native Websocket
- librerie
- Racheal Samson
- Versione: 1.3
- Aggiornato: 4 marzo 2024
- Attivazioni: 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 WSMQL.mqh from here or here (in case MetaTrader downloaded the library 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> // Or Import WebsocketClient.ex5 declarations - Please rename "Native Websocket.ex5" to "WebsocketClient.ex5" //#import "WebSocketClient.ex5" // WSHANDLE Initialize(void); // ENUM_WEBSOCKET_STATE ClientState(WSHANDLE handle); // void SetMaxSendSize(WSHANDLE handle, int max_send_size); // 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, OnMessage callback); // uint OnReceiveBinary(WSHANDLE handle, OnBinaryMessage callback); //#import //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { // Create an instance of the Client CWebSocketClient client; // Or Initialize the client using below method // WSHANDLE handle = WebSocketClient::Initialize(); // Check if the client is initialized if (!client.Initialized()) // or if(handle == 0) return; 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)) // Or if(!WebSocketClient::Connect(handle, url)) return; // Send a string message client.SendString(msg); // Or WebSocketClient::SendString(handle, msg); while (true) { if (IsStopped()) break; // Receive string messages and process them using the OnMessage callback uint frames = client.OnReceiveString(OnMessage); // Or uint frames = WebSocketClient::OnReceiveString(handle, OnMessage); Print("Frames Processed : ", frames); } // Disconnect from the WebSocket server Print("Disconnecting..."); if (client.Disconnect()) // Or if(WebSocketClient::Disconnect(handle)) Print("Disconnected!"); } //+------------------------------------------------------------------+ void OnMessage(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.
Needed some help to work out some kinks with the library and my code. Racheal was quick and attentive to support!