具有本地消息传递和非持久连接的Microsoft Edge Extension不起作用 [英] Microsoft Edge Extension with native messaging and non-persistent connection does not work

查看:224
本文介绍了具有本地消息传递和非持久连接的Microsoft Edge Extension不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了具有本机消息传递的Microsoft Edge扩展,但是我无法使其与非持久连接一起工作.

I have created a Microsoft Edge Extension with native messaging but I cannot make it work with non-persistent connections.

此处给出的示例是在我的特定情况下没有用:

The example given here is not useful in my particular case:

示例(随机数生成器)显示了服务器已在运行时,两个UWP应用程序如何在客户端与服务器进行通信的地方进行通信.在具有本机消息传递的Edge Extensions(我的基于数字签名和安全输入示例)中,我有一个W32应用程序,当消息通过扩展名从页面到达时,服务器未运行.

The example (Random Number Generator) shows how two UWP applications can communicate where the client communicates with the server when the server is already running. In Edge Extensions with Native Messaging (and mine is based on the Digital Signing and Secure Input examples), I have a W32 applications where the server is NOT running when the message arrives from the page via the extension.

我已检出所有我可以在此处找到的示例,并且都没有使用我需要的非持久"连接:

I have checked out all the samples I could find here and none are using "non-persistent" connections which I need:

我也尝试在Edge扩展中使用runtime.sendNativeMessage,但这仅适用于第一条消息.

I have also tried using runtime.sendNativeMessage in my Edge extension but this only works for the first message.

我没有交互式的html页面,用户无法通过该页面开始与runtime.connectNative的连接.这就是为什么它必须是使用本机应用程序中的runtime.sendNativeMessagestart/query/get-response的非持久"连接的方式,类似于Chrome和Firefox使用的方式.

I do not have an interactive html page whereby the user can start a connection with the runtime.connectNative. That's why it has to be a "non-persistent" connection using runtime.sendNativeMessage to start/query/get-response from the native application in a similar way to the one used by Chrome and Firefox.

推荐答案

我现在已经解决了此问题,并将Edge扩展从持久连接扩展转换为非持久连接扩展.

假设您从类似于此处的扩展名开始,将扩展转换为非持久扩展的中心思想如下:

I have now solved this issue and converted my Edge extension from a persistent to a non-persistent connection extension.

Assuming you start with an extension similar to the one here, the central ideas in converting the extension to a non-persistent one are as follows:

  1. 从content.js和background.js中删除所有与连接/断开连接相关的JavaScript代码
  2. 将启动Win32应用程序的调用从OnBackgroundActivated()移动到OnAppServiceRequestReceived()
  3. 最后一步确保我们仅致电

Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync()

,当我们实际上从Edge收到消息时.

, when we actually receive a message from Edge.

  1. 然后使用Edge消息的有效负载调用win32应用程序.在以下调用中,win32应用程序通过AppServiceConnection将结果传递回Edge:

        // Send message to the win32 application component and wait for response
        AppServiceResponse centennialResponse = await this.centennialConnection.SendMessageAsync(args.Request.Message);
        await args.Request.SendResponseAsync(centennialResponse.Message);

  1. 以上步骤导致调用从background.js中的browser.runtime.sendNativeMessage()开始,由OnAppServiceRequestReceived()进行接听.后者启动win32应用程序,并通过centennialConnection中创建的AppServiceConnection将消息传递给它.它等待结果并将响应发送回background.js.
  2. 最后一步中的所有调用都不会在后台脚本和后台任务之间建立会话或持久连接的情况下发生.

由于上述更改,我们扩展的可靠性大大提高了.

The reliability of our extension has improved a great deal as a result of the above changes.

这篇关于具有本地消息传递和非持久连接的Microsoft Edge Extension不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆