forked from AustenConrad/mql4zmq
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmql4zmq_pub.rb
30 lines (25 loc) · 1.24 KB
/
mql4zmq_pub.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# run via: ruby mql4zmq_pub.rb 10.18.16.5:2028 cmd
# Hello World
# => sends message 'cmd Hello World' to the MetaTrader EA at 10.18.16.5:2028
require 'zmq'
# Check for help being requested.
if ARGV[0] == "-h" || ARGV[0] == "--help"
puts "Usage: ruby mql4zmq_pub.rb [MetaTrader IP address]:[MQL4ZMQ EA Port Number default 2028] [channel to send messages on]"
puts "example: \n ruby mql4zmq_pub.rb 10.18.16.16:2028 cmd\n Hello World\n => sends message 'cmd Hello World' to the MetaTrader EA at 10.18.16.16"
else
# Initialize ZeroMQ Context.
context = ZMQ::Context.new
# Initialze channel variable with the supplied channel name to publish to.
chan = ARGV[1]
# Configure the ZeroMQ socket to be of type Publish.
pub = context.socket ZMQ::PUB
# Connect to the Subscription node. This is backwards from the way this is usually done, but we
# do it this way for the MQL4ZMQ project so that we can have multiple publishers send commands to
# the MetaTrader EA.
pub.connect "tcp://#{ARGV[0]}"
# On newline, send the message to the MetaTrader EA.
while msg = STDIN.gets
msg.strip!
pub.send "#{chan} #{msg}"
end
end