Communication between C# and JavaScript with CefGlue

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

Communication between C# and JavaScript with CefGlue

Postby GMoneyCT » Wed Dec 23, 2015 2:56 pm

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!
GMoneyCT
Newbie
 
Posts: 2
Joined: Wed Dec 23, 2015 2:51 pm

Re: Communication between C# and JavaScript with CefGlue

Postby Serginio1 » Fri Feb 03, 2017 7:12 am

Serginio1
Newbie
 
Posts: 8
Joined: Fri Jan 27, 2017 8:26 am

Re: Communication between C# and JavaScript with CefGlue

Postby tgraupmann » Fri Feb 17, 2017 12:05 am

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)
            {
tgraupmann
Techie
 
Posts: 16
Joined: Tue Feb 14, 2017 12:51 pm

Re: Communication between C# and JavaScript with CefGlue

Postby tgraupmann » Fri Feb 17, 2017 3:28 pm

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.
tgraupmann
Techie
 
Posts: 16
Joined: Tue Feb 14, 2017 12:51 pm


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 15 guests