Fivell
Fivell

Reputation: 11929

ruby rack rpc server/client example

I have next code

require 'rack/rpc'

class Server < Rack::RPC::Server
  def hello_world
    "Hello, world!"
  end


  rpc 'hello_world' => :hello_world

end


server = Server.new
use Rack::RPC::Endpoint, server
run server

script name is server.ru

I'm starting server with command

thin start -R server.ru 


>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

Also client example

require "xmlrpc/client"

# Make an object to represent the XML-RPC server.
server = XMLRPC::Client.new( "0.0.0.0", "/rpc", 3000)
# Call the remote server and get our result
result = server.call("hello_word")


puts result

next command gives me exception

ruby client.rb 
client.rb:414:in `call': Method hello_word missing or wrong number of parameters! (XMLRPC::FaultException)
    from client.rb:7:in `<main>'

Why hello_word can't be found? Thank you.

Upvotes: 1

Views: 1284

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54684

This is just a typo. You are calling hello_word instead of hello_world try:

result = server.call("hello_world")

this should do the trick. I do not think that you can start the server with the run server in your Rackup file. Of course you need a run statement in your config.ru but running an instance of your server class is pointless, because it doesn't even have a call method. In practice you would mount another application there, for example:

require 'builder'
require 'rack/rpc'

class Server < Rack::RPC::Server
  def hello_world
    "Hello, world from RPC Server!"
  end 

  rpc 'hello_world' => :hello_world
end

class MyApplication
  def call(env)
    [200, {"Content-Type" => "text/plain"}, ["Hello world from MyApplication!"]]
  end 
end

use Rack::RPC::Endpoint, Server.new
run MyApplication.new

I also needed to include the builder gem in order to get this to work.

Upvotes: 1

Related Questions