Thank you for taking the time to put this together.
I believe I understand what your stating here and have had a similar set up.
onDisconnect event will fire if the connection attempt fails regardless of whether an attempt to post a message to the native application is made. If that was ignored, the attempt to post a message to the native app would fail also, but it returns nothing but
undefined whether successful or not.
I wanted to distinguish between a failed attempt to connect and a dropped connection; so, the text message of the error object appears to be the only way to do that, apart from maybe the
columnNumber, fileName, lineNumber properties. If the process
conhost.exe is ended via the task manager, assuming that closes the native app, the
onDisconnect event doesn’t fire. I need to test what takes place when the native application closes due to programmatic exit or error, and when the extension closes the connection because it received an invalid JSON string. Perhaps, one of the those three properties makes a distinction. I’m pretty sure an invalid string fires the
I was considering providing a message that states, failed to connect, native app error/exited, extension closed the app–please click to try to reconnect. But that distinction is not necessary.
onDisconnect doesn’t fire for all of these cases, then subsequent attempts to communicate with the native application will fail silently and the extension will appear unresponsive. So, it’s a bit more important than just wanting a descriptive error message. But, as I wrote, I’ve a lot more to test and I need more on the native app side to do so properly.
I wondered about when a ping to the native app should be made to confirm that a connection has been established, since it appeared to be asynchronous in regards to being able to test port.error immediately after making the request to open a port. But you can post a message on the port immediately after making the request and it works.
nativeApp.port = browser.runtime.connectNative( "AppName" );
console.log( nativeApp.port.postMessage( "hello" ) );
undefined but it works. Why does it work but the error message is delayed? Perhaps it is queued like writing the indexedDB event handlers after making the database request. I don’t know.