kdeyev / rpc-web-channel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rpc-web-channel JavaScript Client

rpc-web-channel mimics to QtWebChannel JavaScript API, but uses JSON-RPC 2.0 protocol over uni-directional communication channel. In general rpc-web-channel JavaScript Client is the client API for rpc-web-channel server, which builds client-side stubs based on JSON Schema Service Descriptor

Motivation

The regular JSON-RPC 2.0 client code:

var jrpc = new simple_jsonrpc();

//set a communication function
jrpc.toStream = function(_msg){
    ...
};

//call
jrpc.call("object.slot").then(function (result) {
    ...
});

The rpc-web-channel client:

var jrpc = new simple_jsonrpc();

jrpc.toStream = function(_msg){
    ...
};


new rpc-web-channel(jrpc, function(services) {
    services.object.slot().then(function (result) {
        ...
    });
});

Getters/Setters

rpc-web-channel allows the symplyfied access to getters and setter. The regular access looks like:

 // Properties access 
object.setProperty("I'm setter").then(function (result) {
    document.getElementsByClassName('paragraph')[0].innerHTML += 'object.setProperty result: ' + result + '<br>';
    
    // call Getter as function call
    object.getProperty().then(function (result) {
        document.getElementsByClassName('paragraph')[0].innerHTML += 'object.getProperty    result: ' + result + '<br>';
    });
});

And the symplified version:

// Anonymous local async function call is nedded for further await
(async function() {
    // wait for setter finish
    await (object.propertyWithGetterSetter = 42);

    // wait for getter finish
    let property = await object.propertyWithGetterSetter;

    // synch access to property
    document.getElementsByClassName('paragraph')[0].innerHTML += 'propertyWithGetterSetter result: ' + property + '<br>';
}());

References

  • QtWebChannel enables peer-to-peer communication between a server (QML/C++ application) and a client (HTML/JavaScript or QML application)
  • JSON Schema Service Descriptor is simply a JSON Schema with the additional definition for methods.
  • simple-jsonrpc-js - Simple JSON-RPC javascript library. Generate and parse json rpc messages

About

License:MIT License


Languages

Language:JavaScript 100.0%