Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Networking Introduction

Robin Gabriël edited this page Feb 23, 2020 · 3 revisions

Chromely via CefGlue provides 2 different ways of Inter-process Communication (IPC) between the Renderer and the Browser.

  • Generic Message Routing
  • Ajax HTTP/XHR

These allow Chromely to receieve JavaScript requests initated by the Renderer, processed by the Browser (C#) and returned Json data response to the Renderer.

The configuration of an IPC workflow involves:

  1. Create a Controller class
  2. Register an Action or Command method in the Controller class
  3. Register the Controller class
  4. Register a scheme (http, custom, etc)
  5. Create a JavaScript request in the Renderer html

Action methods vs Command methods

  • An action is the normal way to do IPC. There is a request and a response.
  • Commands are fire-and-forget requests. As you might already have guessed, no response is returned.

More information on how to implement Command methodes.

1. Create a Controller class

Every IPC workflow requires a Controller class. The class must inherit ChromelyController.

public class CustomController : ChromelyController
{
}

2. Register an Action method in the Controller class

An action method can be registered in the custom controller constructor or as an attribute decorated method.

  • Constructor type
public DemoController()
{
	RegisterGetRequest("/democontroller/movies", GetMovies);
	RegisterPostRequest("/democontroller/movies", SaveMovies);
}

private ChromelyResponse GetMovies(ChromelyRequest request)
{
}

private ChromelyResponse SaveMovies(ChromelyRequest request)
{
}
  • Attribute decorated type
[HttpGet(Route = "/democontroller/movies")]
public ChromelyResponse GetMovies(ChromelyRequest request)
{
}

[HttpPost(Route = "/democontroller/movies")]
public ChromelyResponse SaveMovies(ChromelyRequest request)
{
}

3. Register the Controller class

The class CustomController must be registered. Registration can be done either by registering the Assembly where the class is created or registering via the Container

  • Registering the assembly class can be done in 2 ways:
  1. Adding the fullpath of the assembly in the configuration config file
"controllerAssemblies": [
      "Chromely.External.Controllers.dll"
],
  1. Adding it in the configuration object.
var config = new DefaultConfiguration();
config.ControllerAssemblies = new List<ControllerAssemblyInfo>();
config.ControllerAssemblies.RegisterServiceAssembly("Chromely.External.Controllers.dll");
  • Register the Controller class via the Container:
public class DemoChromelyApp : ChromelyBasicApp
{
    public override void Configure(IChromelyContainer container)
    {
        base.Configure(container);
        container.RegisterSingleton(typeof(ChromelyController), Guid.NewGuid().ToString(), typeof(CustomController));
    }
}

4. Register a scheme (http, custom, etc)

For details on registering a custom scheme please see Custom Http Registration.

5. Create a JavaScript request in the Renderer html

To trigger an actual request, an event must must be set up in the HTML file.

A sample:

function getMovies() {
    var request = {
        "method": "GET",
        "url": "/democontroller/movies",
        "parameters": null,
        "postData": null
    };
    window.cefQuery({
        request: JSON.stringify(request),
        onSuccess: function (response) {
    -               -- process response
        }, onFailure: function (err, msg) {
            console.log(err, msg);
        }
    });
}