【问题标题】:ruby rack rpc server/client exampleruby rack rpc 服务器/客户端示例
【发布时间】:2023-03-10 21:04:01
【问题描述】:

我有下一个代码

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

脚本名称是 server.ru

我正在使用命令启动服务器

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

也是客户端示例

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

下一个命令给了我异常

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>'

为什么找不到 hello_word?谢谢。

【问题讨论】:

  • 改变你原来的问题有什么意义,这样实际的问题就不再存在了?

标签: ruby xml-rpc rack


【解决方案1】:

这只是一个错字。你打电话给hello_word而不是hello_world试试:

result = server.call("hello_world")

这应该可以解决问题。我不认为您可以使用 Rackup 文件中的 run server 启动服务器。当然,您需要在 config.ru 中使用 run 语句,但运行服务器类的实例是没有意义的,因为它甚至没有 call 方法。在实践中,您会在那里挂载另一个应用程序,例如:

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

我还需要包含 builder gem 才能让它工作。

【讨论】:

猜你喜欢
  • 2012-03-10
  • 1970-01-01
  • 2012-04-29
  • 2016-01-05
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 2021-03-02
相关资源
最近更新 更多