问题
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:
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:
I have also tried using runtime.sendNativeMessage
in my Edge extension but this only works for the first message.
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.
回答1:
There are no separate API's in Edge for persistence and Non-persistence connection. The only difference is: for persistence connection we will keep the connection open after it is successfully established. We can close it till the task is done. Please refer to this sample.
However, for non-persistence connections, we would release the connect as soon as the response is received and stored as an AppServiceResponse. This sample is very clear where we can see the AppServiceConnection object lasts only for one connection.
Hope this helps.
回答2:
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:
- Remove all JavaScript code associated with connect/disconnect from the content.js and background.js
- Move the call that launches the win32 application from OnBackgroundActivated() into OnAppServiceRequestReceived()
- The last step ensures that we only call
Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync()
, when we actually receive a message from Edge.
- The win32 application is then called with the payload of the message from Edge. The win32 application communicates the result back to Edge via the AppServiceConnection in the following calls:
// 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);
- The above steps result in a call that starts with browser.runtime.sendNativeMessage() in background.js which is picked up by OnAppServiceRequestReceived(). The latter launches the win32 application and passes the message to it via the AppServiceConnection made in centennialConnection. It awaits the result and sends the response back to background.js.
- The calls in the last step all happen without a session or a persistent connection ever being established between the background script and the background task.
The reliability of our extension has improved a great deal as a result of the above changes.
来源:https://stackoverflow.com/questions/57576523/microsoft-edge-extension-with-native-messaging-and-non-persistent-connection-doe