Home > Enterprise >  WebView2 - embedded in WinForms - hooking up to WebSocket
WebView2 - embedded in WinForms - hooking up to WebSocket

Time:12-03

Is it possible in C# to hookup to WebView2 control embedded in WinForms by hooking up to DevTools (which I know is possible because I did it) and then, in turn, hooking up to WebSocket, so I can listen to all the responses that are coming back to the WebView2 browser? And if yes, what is the interface that I can use to hookup to WebSocket? I did similar thing with CefSharp browser control, but cannot find socket interface in WebView2...

CodePudding user response:

Yes, it is possible to hook up to the WebView2 control embedded in a WinForms application by hooking up to its DevTools and then to its WebSocket. This would allow you to listen to all the responses that are coming back to the WebView2 browser and handle them as needed.

To hook up to the WebView2 control's DevTools, you can use the WebView2DevToolsProtocolEventReceiver class in the Microsoft.Web.WebView2.Core namespace. This class provides events that you can use to listen for messages from the DevTools, such as ConsoleMessageAdded, NetworkMessageReceived, and PageNavigationCompleted. Here is an example of how you can use this class to hook up to the DevTools:

// Import the WebView2DevToolsProtocolEventReceiver class
using Microsoft.Web.WebView2.Core;

// Create a WebView2DevToolsProtocolEventReceiver object
WebView2DevToolsProtocolEventReceiver devToolsReceiver = new WebView2DevToolsProtocolEventReceiver();

// Listen for messages from the DevTools
devToolsReceiver.ConsoleMessageAdded  = (sender, args) =>
{
    Console.WriteLine($"Console message: {args.Message}");
};

This code creates a WebView2DevToolsProtocolEventReceiver object and listens for ConsoleMessageAdded events, which are triggered whenever a message is added to the DevTools console. When a message is received, it is printed to the console.

Once you are hooked up to the DevTools, you can use the WebSocket class in the System.Net.WebSockets namespace to connect to the WebView2 control's WebSocket. The WebSocket class provides methods for sending and receiving messages over a WebSocket connection, such as ConnectAsync, SendAsync, and ReceiveAsync. Here is an example of how you can use this class to connect to the WebView2 control's WebSocket:

// Import the WebSocket class
using System.Net.WebSockets;

// Create a WebSocket object
WebSocket webSocket = new WebSocket();

// Connect to the WebView2 control's WebSocket
await webSocket.ConnectAsync(new Uri("ws://localhost:9222/devtools/page/1234"));

This code creates a WebSocket object and uses the ConnectAsync() method to connect to the WebView2 control's WebSocket. It specifies the URL of the WebSocket connection, which in this example is ws://localhost:9222/devtools/page/1234. You will need to replace this URL with the actual URL of the WebView2 control's WebSocket, which you can obtain from the DevTools.

Once you are connected to the WebSocket, you can use the SendAsync() and ReceiveAsync() methods to send and receive messages over the WebSocket connection.

  • Related