View on GitHub

Jsonrpc

JSON RPC C Implementation

Download this project as a .zip file Download this project as a tar.gz file

jsonrpC

jsonrpC is an implementation of JSON RPC 2.0 protocol in C.

jsonrpC is licensed under the MIT License

Compilation

$mkdir build
$cd build
$cmake ..
$make
build output left in jsonrpc-x.y

Example

Server

test_websocket.c

jsonrpc_error_t subtract (int argc, const jsonrpc_param_t *argv, void (* print_result)(void *ctx, const char *fmt, ...), void *ctx)
{
    print_result(ctx, "%lf", 
        argv[0].json.u.number - argv[1].json.u.number
    );
    return JSONRPC_ERROR_OK;
}

int main (int argc, const char * argv[])
{
    jsonrpc_server_t *server;
    jsonrpc_error_t   error;

    server = jsonrpc_server_open(
                /* json_plugin: yajl       */jsonrpc_plugin_yajl(), 
                /*  net_plugin: websockets */jsonrpc_plugin_websockets_server(), 
                /* port */8212
            );
    error  = jsonrpc_server_register_method(server, JSONRPC_TRUE, subtract, "subtract", "minuend:i, subtrahend:i");
    // add more method here..

    for (;;)
    {
        error = jsonrpc_server_run(server, 50);
        if (error != JSONRPC_ERROR_OK && error != JSONRPC_ERROR_SERVER_TIMEOUT)
            break;
    }
    jsonrpc_server_close(server);
    return 0;
}

Client

test_websocket.html

var ws;

function init()
{
    ws = new WebSocket("ws://localhost:8212", "jsonrpc-server-websocket");
    ws.onopen = function(e) {
        writeLog('WebSocket: CONNECTED');
    };
    ws.onclose = function(e) {
        writeLog('WebSocket: DISCONNECTED');
    };
    ws.onmessage = function(e) {
        writeLog('<-- ' + e.data);
    };
    ws.onerror = function(e) {
        writeLog('WebSocket: ERROR(' + e.data + ')');
    };
}
function request()
{
    var json=document.getElementById("pre_code").value;

    writeLog('--> ' + json)
    ws.send(json);
}
function writeLog(text)
{
    document.getElementById("pre_output").value += text + '\n';
}

Screenshot

open test_websocket.html (with WebSocket supported browser) screenshot