Google chrome 重新加载扩展时,通过连接本机消息来运行多个本机应用程序

Google chrome 重新加载扩展时,通过连接本机消息来运行多个本机应用程序,google-chrome,google-chrome-extension,chrome-native-messaging,Google Chrome,Google Chrome Extension,Chrome Native Messaging,在chrome extension中,我使用本机消息传递来调用本地应用程序。但我发现了一个问题,即每次重新加载扩展时,似乎都会为应用程序创建一个新的进程。根据文档,如果端口断开或页面关闭,应用程序将结束。这是否意味着重新加载扩展不会关闭background页面?我怎样才能解决这个问题?此外,我无法在chrome任务管理器中找到我的本地应用程序进程 // background.js var port = null; connectToNativeHost(); // Receive messa

在chrome extension中,我使用本机消息传递来调用本地应用程序。但我发现了一个问题,即每次重新加载扩展时,似乎都会为应用程序创建一个新的进程。根据文档,如果
端口
断开或页面关闭,应用程序将结束。这是否意味着重新加载扩展不会关闭
background
页面?我怎样才能解决这个问题?此外,我无法在chrome任务管理器中找到我的本地应用程序进程

// background.js

var port = null;
connectToNativeHost();

// Receive message from other js
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log("background recieved message from " + sender.url + JSON.stringify(request));
        parseMessage(request);
    }
);

//onNativeDisconnect
function onDisconnected()
{
    console.log(chrome.runtime.lastError);
    console.log('disconnected from native app.');
    port = null;
}

// Receive message from native app
function onNativeMessage(message)
{
    console.log('recieved message from native app: ' + JSON.stringify(message));
}

//connect to native host and get the communicatetion port
function connectToNativeHost()
{
    var nativeHostName = 'com.group_project.time_tracker';
    port = chrome.runtime.connectNative(nativeHostName);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
    console.log("connected");
}

// Send message to native app
function sendMessage(message)
{
    port.postMessage(message);
    console.log('send messsage to native app: ' + JSON.stringify(message));
}

假设您查看的是任务管理器中的
进程
选项卡,而不是主选项卡,这意味着实际上没有多个进程。重新加载扩展将终止旧的后台页面上下文并再次运行它。当使用connectNative时,主机应用程序将再次启动。如文档所示,旧的任务应该自动终止。我检查了Windows任务管理器和Chrome任务管理器。目前,我只是写了一个系统托盘图标来表示正在运行的应用程序,我非常确定图标的数量将与“重新加载”的点击次数相同。Windows中仍然存在一个古老的错误,即当进程被杀死时,托盘图标将保持不变,因此这根本不是一个可靠的指示器。可能有一些方法可以在你的应用程序中清理它,可能运行第二个进程,我不知道,谷歌清理。您还需要检查Windows任务管理器的进程选项卡,正如我在第一条评论中提到的。当然不是Chrome的任务管理器。你可能想看看这篇文章:关闭应用程序是你的工作,Chrome不会这么做。