现在,我尝试从MQL4发送实时数据。放入由Python-Flask构建的套接字服务器
我的MQL4使用socket-library-mt4-mt5.mqh。但是我不怎么处理套接字的事件。
我的套接字服务器
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/')
def sessions():
return render_template('index.html')
def messageReceived(methods=['GET', 'POST']):
print('message was received!!!')
@socketio.on('my event')
def handle_my_custom_event(json, methods=['GET', 'POST']):
print('received my event: ' + str(json))
socketio.emit('my response', json, callback = messageReceived)
我的MQL4代码,作为Socket客户端
#define SOCKET_LIBRARY_USE_EVENTS
#include <socket-library-mt4-mt5.mqh>
string Hostname = "127.0.0.1";
ushort ServerPort = "5000";
ClientSocket * glbClientSocket = NULL;
int OnInit()
{
return NULL;
}
void OnDeinit(const int reason)
{
if (glbClientSocket) {
delete glbClientSocket;
glbClientSocket = NULL;
}
}
void OnTick()
{
if (!glbClientSocket) {
glbClientSocket = new ClientSocket(Hostname, ServerPort);
if (glbClientSocket.IsSocketConnected()) {
Print("Client connection succeeded");
} else {
Print("Client connection failed");
}
}
if (glbClientSocket.IsSocketConnected()) {
string strMsg = Symbol() + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_BID), 6) + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_ASK), 6) + " Hello from MQL \n";
glbClientSocket.Send(strMsg);
} else {
}
if (!glbClientSocket.IsSocketConnected()) {
// Destroy the server socket. A new connection
// will be attempted on the next tick
Print("Client disconnected. Will retry.");
delete glbClientSocket;
glbClientSocket = NULL;
}
}
我检查连接是否正常,但方法仍然错误 如您所见,我的MQL4代码没有handle事件函数
127.0.0.1 - - [2019-08-29 13:25:36] "AUDUSD,0.672250,0.672360 Hello from MQL" 400 - 0.000403
<socket fileno=10 sock=127.0.0.1:5000 peer=127.0.0.1:37348>: Invalid HTTP method: 'AUDUSD,0.672260,0.672360 Hello from MQL \n'
问题是如何处理javascript中的socket.on('my event')之类的事件。进入我的MQL4代码。