Page 1 of 1

Communication between C# and JavaScript with CefGlue

PostPosted: Wed Dec 23, 2015 2:56 pm
by GMoneyCT
Greetings everyone,

Does anyone know of a simple, complete example showing how to communicate between a host C# application and the JavaScript code that runs in the CefGlue browser? I have spent several days and looked at dozens of Google pages but have not found anything that I could fully understand. I am literally trying to send a single string of data both ways - that's it. No other special requirements. Any help would be greatly appreciated. Thanks!

Re: Communication between C# and JavaScript with CefGlue

PostPosted: Fri Feb 03, 2017 7:12 am
by Serginio1

Re: Communication between C# and JavaScript with CefGlue

PostPosted: Fri Feb 17, 2017 12:05 am
by tgraupmann
So far all the examples are C++. It looks like the process is described here (in C++).
https://bitbucket.org/chromiumembedded/ ... t-bindings

MessageRouter Readme:
https://bitbucket.org/xilium/xilium.cef ... ?at=master

Xilium.CefGlue.Wrapper.CefMessageRouterBrowserSide is already part of the DemoApp.

Code: Select all
    public abstract class DemoApp : IDisposable
    {
        public static CefMessageRouterBrowserSide BrowserMessageRouter { get; private set; }


https://bitbucket.org/xilium/xilium.cef ... oApp.cs-12

The DemoApp registers the MessageRouter.

Code: Select all
        private void RegisterMessageRouter()
        {
            if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
            {
                PostTask(CefThreadId.UI, this.RegisterMessageRouter);
                return;
            }

            // window.cefQuery({ request: 'my_request', onSuccess: function(response) { console.log(response); }, onFailure: function(err,msg) { console.log(err, msg); } });
            DemoApp.BrowserMessageRouter = new CefMessageRouterBrowserSide(new CefMessageRouterConfig());
            DemoApp.BrowserMessageRouter.AddHandler(new DemoMessageRouterHandler());
        }


You can test that JS is able to call the DemoApp by opening the developer console and entering the following command.

Code: Select all
window.cefQuery({ request: 'noanswer', onSuccess: function(response) { console.log(response); }, onFailure: function(err,msg) { console.log(err, msg); } });


That causes the DemoMessageRouterHandler in DemoApp to be invoked on the C# side.

Code: Select all
        private class DemoMessageRouterHandler : CefMessageRouterBrowserSide.Handler
        {
            public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
            {

Re: Communication between C# and JavaScript with CefGlue

PostPosted: Fri Feb 17, 2017 3:28 pm
by tgraupmann
Here's another project source example where C# and JavaScript are working both ways.
https://bitbucket.org/tgraupmann/xilium ... at=default

I setup speech detection on a proxy.