This gem implements a very simple JSON RPC 2.0 client and server which uses zeroMQ for transport.
Please install libzmq
, e.g. via apt-get install libzmq3-dev
.
Let's not talk too much, let's see some code:
require 'rubygems'
require 'zmqjsonrpc'
# client request to a running server
client = ZmqJsonRpc::Client.new("tcp://127.0.0.1:49200")
client.some_method(1, "b", [1,{a:1}])
# -or- a client with logger and error handling
require 'logger'
logger = Logger.new(STDOUT)
client = ZmqJsonRpc::Client.new("tcp://127.0.0.1:49200", 10000, loggger)
begin
client.faulty_method()
rescue ZmqJsonRpc::ClientError => e
puts "bad things can happen..."
end
require 'rubygems'
require 'zmqjsonrpc'
class Proxy
def some_method(a,b,c)
# do you thing
return ["xyz", 77]
end
end
# blocking server
proxy = Proxy.new()
server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200")
server.server_loop
# -or- a server with your own logger
require 'logger'
logger = Logger.new(STDOUT)
# ...
server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200", logger)
# -or- dispatch a thread
server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200")
thread = Thread.new {
server.server_loop
}
# and cancel the thread if you want to shut the server down
thread.exit
- API reference documentation
- The JSON RPC 2.0 Spec
- The used ZeroMQ gem and good examples
- Gem making
- Add support for by-name parameters (see the spec)
- Add individual exception classes in the client.
- Send different error codes if something goes wrong in the server.
- Keep the client connection alive instead of re-establishing every time.
- Add more tests.
This code is released under the terms of MIT License.
Please do so! Just send a message or send a pull request. Especially, adding webrick for transport would be nice.