项目作者: zeromq

项目描述 :
mruby bindings for libzmq (v4)
高级语言: C
项目地址: git://github.com/zeromq/mruby-zmq.git
创建时间: 2016-12-31T12:11:39Z
项目社区:https://github.com/zeromq/mruby-zmq

开源协议:Mozilla Public License 2.0

下载


mruby-zmq

mruby bindings for https://github.com/zeromq/libzmq (v4)

Everything libzmq offers is mapped 1:1 to the LibZMQ namespace, no beautification was done.
High level ruby functions are inside the ZMQ Namespace, you can find most of it inside the mrblib folder, except some of the Msg and Socket classes functions which have to be done in c.

Installation

If you dont have libzmq installed on a non Windows platform we will build it and statically link it into your mruby app.
If you have it installed you need to have pkg-config installed.
Then add

  1. conf.gem mgem: 'mruby-zmq'

to your build_config.rb

Examples

Sockets

Client to Server

  1. server = ZMQ::Server.new("tcp://127.0.0.1:*") # you can optionally add a boolean argument if it should connect instead of bind
  2. client = ZMQ::Client.new(server.last_endpoint) # you can optionally add a boolean argument if it should bind instead of connect
  3. client.send("hello")
  4. msg = server.recv
  5. puts msg['Peer-Address'] # This gets you the metadata of a message
  6. puts msg.to_str
  7. msg2 = ZMQ::Msg.new("hello too")
  8. msg2.routing_id = msg.routing_id
  9. msg2.send(server)
  10. puts client.recv.to_str

Dealer to Router

  1. router = ZMQ::Router.new("tcp://127.0.0.1:*")
  2. dealer = ZMQ::Dealer.new(router.last_endpoint)
  3. dealer.send("hello")
  4. msg = router.recv
  5. puts msg[1].to_str
  6. router.send([msg[0], "hello too"])
  7. puts dealer.recv.to_str

Pub to Sub

  1. pub = ZMQ::Pub.new("tcp://127.0.0.1:*")
  2. sub = ZMQ::Sub.new(pub.last_endpoint, "testme")
  3. # you have to wait a short time here because of the slow joiner Problem
  4. pub.send("testme hallo")
  5. puts sub.recv.to_str

Logging

You can define a environment variable called ZMQ_LOGGER_ENDPOINT to create pub sockets which connect to that endpoint.
There is also a ZMQ_LOGGER_IDENT env var which adds a ident to each msg from ZMQ.logger

  1. ZMQ.logger.info("hallo")

LICENSE

Copyright 2017,2021 Hendrik Beskow

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
project, You can obtain one at http://mozilla.org/MPL/2.0/.

Contribution Policy

mruby-zmq follows the C4 Process: https://rfc.zeromq.org/spec:42/C4