使用 (NodeJS + Socket.io) 创建客户端套接字(Winsock、MQL5、MT5)和服务器端套接字
Create Client side socket (Winsock, MQL5, MT5) and Server side Socket using (NodeJS + Socket.io)
我有一个 windows 服务器 2016,我正在尝试使用 EA 将数据从 Meta Trader 5 发送到本地主机中的特定端口 (777),我正在使用 NodeJS,Socket.io另一只手捕捉数据。
我想将数据从 MT5 传递到 localhost:some_port。我从未使用套接字(客户端-服务器)在两个应用程序或代码库之间发送和接收数据。
此处,Web(充当服务器端的 NodeJS 应用程序)和 MT5(充当客户端)。
NodeJS 应用程序 - 创建服务器并查找客户端连接
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var express = require('express');
io.on('connection', function(socket) {
console.log("new user connected");
//sends test data on creating a connection
socket.emit('live-data', 'test data');
});
app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(777, function(){
console.log('listening on *:777');
});
客户端
/* ###################################################################
Example socket client.
Code can be used as both MQ4 and MQ5 (on both 32-bit and 64-bit MT5)
Simply sends each new tick to the server, as a CRLF-terminated
message. The example server then writes these to its Experts log.
################################################################### */
#property strict
// --------------------------------------------------------------------
// Include socket library
// --------------------------------------------------------------------
#include <socket-library-mt4-mt5.mqh>
// --------------------------------------------------------------------
// EA user inputs
// --------------------------------------------------------------------
input string Hostname = "localhost"; // Server hostname or IP address
input ushort ServerPort = 777; // Server port
// --------------------------------------------------------------------
// Global variables and constants
// --------------------------------------------------------------------
ClientSocket * glbClientSocket = NULL;
// --------------------------------------------------------------------
// Initialisation (no action required)
// --------------------------------------------------------------------
void OnInit() {}
// --------------------------------------------------------------------
// Termination - free the client socket, if created
// --------------------------------------------------------------------
void OnDeinit(const int reason)
{
if (glbClientSocket) {
delete glbClientSocket;
glbClientSocket = NULL;
}
}
// --------------------------------------------------------------------
// Tick handling - set up a connection, if none already active,
// and send the current price quote
// --------------------------------------------------------------------
void OnTick()
{
if (!glbClientSocket) {
glbClientSocket = new ClientSocket(Hostname, ServerPort);
if (glbClientSocket.IsSocketConnected()) {
Print("Client connection succeeded");
} else {
Print("Client connection failed");
}
}
if (glbClientSocket.IsSocketConnected()) {
// Send the current price as a CRLF-terminated message
string strMsg = Symbol() + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_BID), 6) + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_ASK), 6) + "\r\n";
glbClientSocket.Send(strMsg);
} else {
// Either the connection above failed, or the socket has been closed since an earlier
// connection. We handle this in the next block of code...
}
// If the socket is closed, destroy it, and attempt a new connection
// on the next call to OnTick()
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;
}
}
更新
我不断收到上述消息。而且我没有看到任何数据发送到 Nodejs 套接字。
问题是您一方面使用套接字,另一方面使用 WebSockets,两者是不同的东西。
在 node 中,您应该使用 net,它是标准库的一部分。
我有一个 windows 服务器 2016,我正在尝试使用 EA 将数据从 Meta Trader 5 发送到本地主机中的特定端口 (777),我正在使用 NodeJS,Socket.io另一只手捕捉数据。
我想将数据从 MT5 传递到 localhost:some_port。我从未使用套接字(客户端-服务器)在两个应用程序或代码库之间发送和接收数据。
此处,Web(充当服务器端的 NodeJS 应用程序)和 MT5(充当客户端)。
NodeJS 应用程序 - 创建服务器并查找客户端连接
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var express = require('express');
io.on('connection', function(socket) {
console.log("new user connected");
//sends test data on creating a connection
socket.emit('live-data', 'test data');
});
app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(777, function(){
console.log('listening on *:777');
});
客户端
/* ###################################################################
Example socket client.
Code can be used as both MQ4 and MQ5 (on both 32-bit and 64-bit MT5)
Simply sends each new tick to the server, as a CRLF-terminated
message. The example server then writes these to its Experts log.
################################################################### */
#property strict
// --------------------------------------------------------------------
// Include socket library
// --------------------------------------------------------------------
#include <socket-library-mt4-mt5.mqh>
// --------------------------------------------------------------------
// EA user inputs
// --------------------------------------------------------------------
input string Hostname = "localhost"; // Server hostname or IP address
input ushort ServerPort = 777; // Server port
// --------------------------------------------------------------------
// Global variables and constants
// --------------------------------------------------------------------
ClientSocket * glbClientSocket = NULL;
// --------------------------------------------------------------------
// Initialisation (no action required)
// --------------------------------------------------------------------
void OnInit() {}
// --------------------------------------------------------------------
// Termination - free the client socket, if created
// --------------------------------------------------------------------
void OnDeinit(const int reason)
{
if (glbClientSocket) {
delete glbClientSocket;
glbClientSocket = NULL;
}
}
// --------------------------------------------------------------------
// Tick handling - set up a connection, if none already active,
// and send the current price quote
// --------------------------------------------------------------------
void OnTick()
{
if (!glbClientSocket) {
glbClientSocket = new ClientSocket(Hostname, ServerPort);
if (glbClientSocket.IsSocketConnected()) {
Print("Client connection succeeded");
} else {
Print("Client connection failed");
}
}
if (glbClientSocket.IsSocketConnected()) {
// Send the current price as a CRLF-terminated message
string strMsg = Symbol() + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_BID), 6) + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_ASK), 6) + "\r\n";
glbClientSocket.Send(strMsg);
} else {
// Either the connection above failed, or the socket has been closed since an earlier
// connection. We handle this in the next block of code...
}
// If the socket is closed, destroy it, and attempt a new connection
// on the next call to OnTick()
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;
}
}
更新
我不断收到上述消息。而且我没有看到任何数据发送到 Nodejs 套接字。
问题是您一方面使用套接字,另一方面使用 WebSockets,两者是不同的东西。
在 node 中,您应该使用 net,它是标准库的一部分。