Class: EZMQ::Socket
- Inherits:
-
Object
- Object
- EZMQ::Socket
- Defined in:
- lib/ezmq.rb
Overview
Wrapper class to simplify 0MQ sockets.
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) context
Returns the value of attribute context.
-
- (Object) decode
Returns the value of attribute decode.
-
- (Object) encode
Returns the value of attribute encode.
-
- (Object) socket
Returns the value of attribute socket.
Instance Method Summary (collapse)
-
- (Boolean) bind(address: 'tcp://127.0.0.1:5555')
Binds the socket to the given address.
-
- (Boolean) connect(address: 'tcp://127.0.0.1:5555')
Connects the socket to the given address.
-
- (Socket) initialize(mode, type, **options)
constructor
Creates a 0MQ socket.
-
- (void) receive(decode: @decode)
Receive a message from the socket.
-
- (Fixnum) send(message = '', encode: @encode)
Sends a message on the socket.
Constructor Details
- (Socket) initialize(mode, type, **options)
Creates a 0MQ socket.
24 25 26 27 28 29 30 31 |
# File 'lib/ezmq.rb', line 24 def initialize(mode, type, **) fail ArgumentError unless [:bind, :connect].include? mode @context = [:context] || ZMQ::Context.new @socket = @context.socket type @encode = [:encode] || -> m { m } @decode = [:decode] || -> m { m } method(mode).call address: [:address] || 'tcp://127.0.0.1:5555' end |
Instance Attribute Details
- (Object) context
Returns the value of attribute context
7 8 9 |
# File 'lib/ezmq.rb', line 7 def context @context end |
- (Object) decode
Returns the value of attribute decode
7 8 9 |
# File 'lib/ezmq.rb', line 7 def decode @decode end |
- (Object) encode
Returns the value of attribute encode
7 8 9 |
# File 'lib/ezmq.rb', line 7 def encode @encode end |
- (Object) socket
Returns the value of attribute socket
7 8 9 |
# File 'lib/ezmq.rb', line 7 def socket @socket end |
Instance Method Details
- (Boolean) bind(address: 'tcp://127.0.0.1:5555')
'localhost' does not always work as expected. Prefer '127.0.0.1'
Binds the socket to the given address.
69 70 71 |
# File 'lib/ezmq.rb', line 69 def bind(address: 'tcp://127.0.0.1:5555') @socket.bind(address) == 0 ? true : false end |
- (Boolean) connect(address: 'tcp://127.0.0.1:5555')
Connects the socket to the given address.
80 81 82 |
# File 'lib/ezmq.rb', line 80 def connect(address: 'tcp://127.0.0.1:5555') @socket.connect(address) == 0 ? true : false end |
- (void) receive(decode: @decode)
This method blocks until a message arrives.
This method returns an undefined value.
Receive a message from the socket.
41 42 43 44 45 |
# File 'lib/ezmq.rb', line 41 def receive(decode: @decode) = '' @socket.recv_string decode.call end |
- (Fixnum) send(message = '', encode: @encode)
If `message` is not a String, `encode` must convert it to one.
Sends a message on the socket.
56 57 58 |
# File 'lib/ezmq.rb', line 56 def send( = '', encode: @encode) @socket.send_string encode.call end |