Welcome to pcjsonrpc Documentation
pcjsonrpc is a powerful and lightweight JSON-RPC framework for Swift. It allows you to easily implement and consume JSON-RPC 2.0 compliant services in your Swift applications.
Getting Started
Installation
You can install pcjsonrpc using CocoaPods by adding the following line to your Podfile:
pod 'pcjsonrpc', '~> 1.0'
Alternatively, you can manually integrate pcjsonrpc into your project by including the pcjsonrpc.swift
file.
Usage
Server Example
// Import pcjsonrpc module
import pcjsonrpc
// Define a service object
let myService = MyService()
// Create a JSON-RPC server
let server = JSONRPCServer(service: myService)
// Start the server
server.start(port: 8080)
Client Example
// Import pcjsonrpc module
import pcjsonrpc
// Create a JSON-RPC client
let client = JSONRPCClient(url: "http://localhost:8080")
// Send a request
client.sendRequest(method: "add", params: [1, 2]) { (response, error) in
if let result = response?["result"] as? Int {
print("Result: \(result)")
} else {
print("Error: \(error?.localizedDescription)")
}
}
Documentation
Server
The server class allows you to create a JSON-RPC server and register your service objects.
Methods
init(service: Any)
: Initializes the server with the provided service object.start(port: Int)
: Starts the server on the specified port number.stop()
: Stops the server.
Client
The client class allows you to send JSON-RPC requests to a server and receive responses.
Methods
init(url: String)
: Initializes the client with the specified server URL.sendRequest(method: String, params: [Any], completion: @escaping (Any?, Error?) -> Void)
: Sends a JSON-RPC request to the server.
Service Object
A service object is a class or struct that exposes methods to be called remotely via JSON-RPC.
Methods
func methodName(param1: Any, param2: Any, ...)
: Description of the method.
pcjsonrpc is a convenient framework for implementing and consuming JSON-RPC services in your Swift applications. It provides an easy-to-use API for creating servers, clients, and service objects.