Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

RPC generator

branch: master

This branch is 0 commits ahead and 0 commits behind master

README.rdoc

rpcoder

installation

$ gem install rpcoder

ruby code for Unity3d RPC

#!/usr/bin/env ruby

require 'rpcoder'
require 'fileutils'

RPCoder.name_space = 'foo.bar.rpc'
RPCoder.api_class_name = 'RPC'

RPCoder.type "Mail" do |t|
  t.add_field :subject, :String
  t.add_field :body,    :String
end

RPCoder.enum "EnumSample1" do |e|
  e.description = "Enumの例"
  e.add_constant :foo, {:desc => "foo"} # 0
  e.add_constant :bar, {:desc => "bar"} # 1
  e.add_constant :baz, {:desc => "baz"} # 2
end

RPCoder.enum "EnumSample2" do |e|
  e.description = "Enumの例"
  e.add_constant :foo, {num: => 1, :desc => "foo"} # 1
  e.add_constant :bar, {num: => 10, :desc => "bar"} # 10
  e.add_constant :baz, {num: => 100, :desc => "baz"} # 100
end

RPCoder.enum "FlagSample" do |e|
  e.description = "ビットフィールドフラグ"
  e.flags = true # true でビットフィールドになる。 default: false
  e.add_constant :root # 1
  e.add_constant :leaf # 2
  e.add_constant :branch # 4
  e.add_constant :apple # 8
end

RPCoder.function "echoEnum" do |f|
  f.path        = "/echo/
  f.method      = "POST"
  f.add_param  :enum1, "EnumSample1" # 必須要素
  f.add_param  :nullable_enum1, "EnumSample1", {:nullable => true} # 省略可能な要素 :nullableオプションはenum専用
  f.add_return_type :enum, "EnumSample1"
  f.description = 'メールを取得'
end

RPCoder.function "getMail" do |f|
  f.path        = "/mails/:id" # => ("/mails/" + id)
  f.method      = "GET"
  f.add_return_type :mail, "Mail"
  f.add_param  :id, "int"
  f.description = 'メールを取得'
end

RPCoder.function "getMails" do |f|
  f.path        = "/mails"
  f.method      = "GET"
  f.add_return_type :mails, "Mail", {:array? => true}
  f.description = 'メールを送信'
end

RPCoder.function "sendMail" do |f|
  f.path        = "/mails/create"
  f.method      = "POST"
  f.add_param  :subject, "String"
  f.add_param  :body,    "String"
  f.description = 'メールを送信'
end

# output codes
dir = File.expand_path('src', File.dirname(__FILE__))
RPCoder.export(dir)

Unity3d C# code for using RPC

var rpc = new RPC('http://localhost:3000');

rpc.getMail(1, (GetMailResponse res) => 
{
  // success
  Debug.Log(res.Mail.Subject));
}, (RpcoderError error) =>
{
  // failure
});

rpc.getMails((GetMailsResponse res) =>
{
  // success
  for each (var mail in res.Mails)
  {
    Debug.Log(mail);
  }
});

dummy server

Dummy server works standalone. You can set dummy logic at callback.

RPCInterface rpc = new RPCDummyServer();
(rpc as RPCDummyServer).getMailLogic = (GetMailRequest req) =>
{
  if(req.Id == 1)
  {
    req.Success(new GetMailResponse(){ Mail = new Mail("subject", "body")})
  }
  else
  {
    req.Error(RPC.RELOAD, "not found", null);
  }
};

rpc.getMail(1, (GetMailResponse res) => Debug.Log(res.Mail.Subject));
Something went wrong with that request. Please try again.