Is it possible to send a message from a WPF app that is hosting a CefSharp control, to the web app running in CefSharp like WebView2 can do - wpf

With WebVeiw2 you can send a message to a web app running in it using WebView2Ctrl?.CoreWebView2?.PostWebMessageAsJson(message).
Is there a way of doing this in CefSharp

Create a class (I used JavascriptCallbackMessenger) to Set and Run the callbacks.
public class JavascriptCallbackMessenger
{
private IJavascriptCallback _callback;
public void SetCallBack(IJavascriptCallback callback)
{
_callback = callback;
}
public void RunCallback(string message)
{
if (_callback != null && _callback.CanExecute)
{
_callback.ExecuteAsync(message);
}
}
}
Create an instance of JavascriptCallbackMessenger and register it with the CefSharp control
CefSharpCtrl.JavascriptObjectRepository.Register(JavascriptCallbackMessengerName, _messenger, true, BindingOptions.DefaultBinder);
Set the callback in Javascript as follows (I'm not a JS developer, but this was my solution).
(async function() {
const cefSharp = (window as any).CefSharp;
await cefSharp.BindObjectAsync(JavascriptCallbackMessengerName);
window.javascriptCallbackMessenger.setCallBack(function(message: string)
{
console.log("messageHandler: " + message);
})
})();
I was using typescript, so I had to extend the Window with the newly created variable.
declare global {
interface Window { javascriptCallbackMessenger: any; }
}
Apologies, but the formatting seems to be a bit "off"!

Related

How to properly configure websocket with springboot and reactjs?

I can establish a websocket connection with my springboot server but I can't access the endpoint from #MessageMapping when I'm trying to send a message. Here are my configurations:
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/simulator")
.setAllowedOrigins("http://myiphere:3000")
.withSockJS();
}
#Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/endpoint");
registry.setApplicationDestinationPrefixes("/app");
}
And a simple controller :
#RestController
#RequestMapping("/api")
public class MyController {
#MessageMapping("/hello/")
#SendTo("/endpoint/greeting")
public Greeting getCurrentLocation() {
System.out.println("hello here");
return GenericBuilder.of(Greeting::new)
.with(Greeting::setContent, "hello from server")
.build();
}
}
I'm using the socketjs-client library in ReactJS by following this tutorial :
import SockJS from "sockjs-client";
import Stomp from "stompjs";
let stompClient;
const connect = () => {
const socket = new SockJS("http://myiphere:8081/simulator");
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("Connected " + frame);
stompClient.subscribe("http://myiphere:8081/endpoint/greeting", function (greeting) {
console.log("hi" + JSON.parse(greeting.body).content);
});
});
};
const sendSomething = () => {
stompClient.send("http://myiphere:8081/app/hello/", {});
};
And some buttons with onClick events bound to the methods above. The connection is working, I'm getting "connected" messages in browser console but when I'm trying to click the button with sendSomething() I'm not getting anything in the browser's console nor server's console.
Solved.
The problem was the absolute url path in the send() method.
P.S.: And I've been looking for an answer for this problem on many sites and found out that there is no need to use absolute path for subscribe() url.
P.P.S.: In case that someone else have these problems, look for extra / too. You have to be careful when you're setting the url. The pattern from JS should match the one from SpringBoot.

Cannot get an IdentityServer4 custom cookie handler working

I'm unable to get a custom cookie authentication handler working with IdentityServer4. I'm using ASP.NET Core Identity and have followed the official guide: https://identityserver4.readthedocs.io/en/release/topics/signin.html
I need to override the CookieAuthenticationEvents.ValidatePrincipal and CookieAuthenticationEvents.SignedIn event handlers.
I've written a class that inherits CookieAuthenticationEvents and overrides the two event handlers.
I'm assigning it to a custom cookie handler via:
var auth = services.AddAuthentication("MyCookies");
auth.AddCookie("MyCookies", options =>
{
options.Events = new RealtimeStatusCookieAuthEvents(Configuration);
});
Here's my code:
https://gist.github.com/Amethi/f3411038a9447d274c0b721698fc5e63
The event handlers don't fire, i.e. I'm expecting them to fire for each request (due to ValidatePrincipal) and when I come back to the site after closing the browser and sign-in using cookie authentication (SignedIn).
Anyone know what I'm doing wrong?
Update:
Even simplifying it as follows doesn't help. The event handlers don't fire.
var auth = services.AddAuthentication("CustomCookies").AddCookie("CustomCookies", options =>
{
options.Events = new CookieAuthenticationEvents
{
OnSignedIn = context =>
{
Console.WriteLine("{0} - {1}: {2}", DateTime.Now,
"OnSignedIn", context.Principal.Identity.Name);
return Task.CompletedTask;
},
OnValidatePrincipal = context =>
{
Console.WriteLine("{0} - {1}: {2}", DateTime.Now,
"OnValidatePrincipal", context.Principal.Identity.Name);
return Task.CompletedTask;
},
};
});
I managed to make my custom cookie authentication handler work by using the ConfigureApplicationCookie extension.
builder.Services.ConfigureApplicationCookie(config =>
{
config.Cookie.Name = "IdentityServer.Cookie";
config.EventsType = typeof(CustomCookieAuthenticationHandler);
config.LoginPath = "/Account/Login";
});
And register the CustomCookieAuthenticationHandler handler
builder.Services.AddScoped<CustomCookieAuthenticationHandler>();
This is the handler implementation:
public class CustomCookieAuthenticationHandler: CookieAuthenticationEvents
{
private readonly IUserRepository _userRepository;
public CustomCookieAuthenticationEvents(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public override Task ValidatePrincipal(CookieValidatePrincipalContext context)
{
// Your cookie authentication logic.
}
}
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-6.0

CefSharp: Injecting custom CSS File using a custom scheme

I'm using CefSharp (47) to render a webpage from a host that I have no control over, and I want to make some additional CSS tweaks to those provided by the host.
Reading up on various topics across GitHub (https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp.Example/CefSharpSchemeHandlerFactory.cs), and here (CefSharp custom SchemeHandler), I wrote a custom scheme handler accordingly:
public class CustomSchemeHandlerFactory : ISchemeHandlerFactory
{
public const string SchemeName = "custom";
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
Console.WriteLine(request.Url);
if (schemeName.ToLower() == SchemeName.ToLower())
{
// Do some stuff
}
return null;
}
}
I attempt to bind it in my application in the following manner:
CefSettings settings = new CefSettings();
settings.CachePath = browserCachePath;
settings.RegisterScheme(new CefCustomScheme()
{
SchemeName = CustomSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CustomSchemeHandlerFactory()
});
Cef.Initialize(settings);
The application then browses to the appropriate website, and uses the 'LoadingStateChanged' event to then fire off some JavaScript to inject the CSS file I want to load:
string linkText = "<link rel=\u0022stylesheet\u0022 type=\u0022text/css\u0022 href=\u0022custom://custom.css\u0022>";
var jsFunctionText = string.Format("(function() {{ $('head').append('{0}'); return true;}}) ();", linkText);
var injectionTask = await _myBrowser.GetMainFrame().EvaluateScriptAsync(jsFunctionText, null);
...which succeeds.
But my custom resource handler 'Create' event is never fired.
I can only presume that the handler isn't being registered properly, so I'd appreciate any advice/help in getting this working properly!
Thanks!

Flex Mobile Event Listener not firing

I'm having trouble getting an event listener to work in a mobile app (Built in Flash Builder 4.5, Flex SDK 4.5.1)
I have an event class called BMS_Event.as which looks like this:
package model
{
import flash.events.Event;
public class BMS_Event extends Event
{
public static var COMPLETE_EVENT:String = "BMSData_Complete";
public static var FAULT:String = "BMSDatafault";
public var data:*;
public function BMS_Event(type:String, data:*=null, bubbles:Boolean=false, cancelable:Boolean=false)
{
this.data = data;
super(type, bubbles, cancelable);
}
}
}
A class to dispatch the event:
package model
{
import flash.events.Event;
import flash.events.EventDispatcher;
import model.BMS_Event;
public class BMSDataParser extends EventDispatcher
{
public function BMSDataParser()
{
trace("BMSDataParser function");
var BMSDataCompleteEvent:BMS_Event = new BMS_Event(BMS_Event.COMPLETE_EVENT);
dispatchEvent(BMSDataCompleteEvent);
}
}
}
And in my Mobile App view, two functions to call the eventparser, and then an eventlistener which listens for the complete event:
import model.BMSDataParser;
import model.BMS_Event;
protected function getData():void
{
var parser:BMSDataParser = new BMSDataParser();
parser.addEventListener(BMS_Event.COMPLETE_EVENT, bmstest);
}
private function bmstest(e:BMS_Event):void
{
trace("bmstest function");
}
The problem I'm having is that the event listener isn't firing, everything works fine up until that point. It does work in a web application, but for whatever reason not a mobile app.
I'm new to mobile app development - is this a limitation of AIR mobile Apps?
Any help/suggestions greatly appreciated.
Thanks
Maybe make var parser:BMSDataParser a private class variable, and in the creationComplete or initialized events of the class add parser.addEventListener(BMS_Event.COMPLETE_EVENT, bmstest);
One more thing: if your using ViewNavigatorApplication with Views, the views are not created until they are pushed onto the view stack e.g. navigator.pushView(PayNowView);, so any listeners will not be activated unless they are displayed first

Unit testing with MVVM Light & DispatcherHelper

I have a SL4 app that utilizes the MVVM Light Toolkit. Within a view model, I call a data service that retrieves data from an OData service. Within the VM, I am using the DispatcherHelper utility class (part of MVVM Light) to update the property on the VM from the data in the callback I pass into the data service. For instance, my view model method looks like this:
public string CurrentUserLogin {
get {
if (string.IsNullOrEmpty(_currentUserLogin))
RetrieveCurrentUserLogin();
return string.IsNullOrEmpty(_currentUserLogin) ? _currentUserLogin : _currentUserLogin.Replace(#"\\", #"\");
}
set {
if (_currentUserLogin != value) {
_currentUserLogin = value;
RaisePropertyChanged(CurrentUserLoginPropertyName);
}
}
}
private void RetrieveCurrentUserLogin() {
DataService.GetCurrentUserLogin(result => {
DispatcherHelper.UIDispatcher.BeginInvoke(() => {
CurrentUserLogin = result;
});
});
}
And here's what my data service looks like:
public void GetCurrentUserLogin(Action<string> callback) {
// create query request
var query = OnDemandContext.CreateQuery<string>("GetCurrentUserLogin");
var request = (HttpWebRequest)WebRequest.Create(query.RequestUri);
request.BeginGetResponse(asyncResult => {
var responseStream = request.EndGetResponse(asyncResult).GetResponseStream();
var responseDocument = XDocument.Load(responseStream);
callback(responseDocument.Root.Value);
}, null);
}
Everything works great when I run my SL application. However the problem I have is when I try to write unit tests against it using the SL Unit Testing Framework. I can test my data service without an issue, but it seems the DispatcherHelper is throwing a wrench into all my tests as the DispatcherHelper.UIDispatcher is always null when fired. I'm assuming it has something to do with the initlization (which is in my SL app's Application_Startup()). I tried initializing it in my test app but that isn't helping. I've also tried using DispatcherHelper.CheckBeginInvokeOnUI() but that has no effect on the issue.
Ideas?
AC,
I just created a simple SL UT project and I did this in the App.XAML.CS
private void Application_Startup(object sender, StartupEventArgs e)
{
RootVisual = UnitTestSystem.CreateTestPage();
DispatcherHelper.Initialize();
}
Then I set this as the test (in the tests.cs):
[TestMethod]
public void TestMethod1()
{
Assert.IsNotNull(DispatcherHelper.UIDispatcher, "UI Dispatcher should not be null");
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
// Do nothing
var x = 1;
});
}
That worked for me. I even set a break point on the "var x = 1;" and it hit the breakpoint. Does this solve your problem? (if so please mark it as the answer).

Resources