让Chrome Packaged应用程序在后台运行? [英] Keep Chrome Packaged App running in background?

查看:202
本文介绍了让Chrome Packaged应用程序在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建Chrome应用和/或扩展程序,以便与串行设备进行通信(使用 https ://developer.chrome.com/apps/serial ),用于在网页上显示设备中的数据。



我创建了一个应用程序可以从我的设备读取消息,但我现在需要将这些消息发送到我的网页。我的计划是创建一个与我的应用进行通信的扩展,然后更新我的网页。我的扩展使用chrome.runtime.sendMessage将消息发送到应用程序,然后我的应用程序用包含来自我的设备的信息的消息作出响应。

当应用程序正在运行时,此工作正常,但当我关闭应用程序时,它会停止侦听新消息。 是否有任何方法可以让我的应用始终保持听取新消息?

我创建应用的原因(而不仅仅是扩展名)从一开始就是因为扩展不允许与串行设备进行通信。是否还有其他方法可以构建应用和扩展程序?我可以实现我想要的功能吗?

更新:

我刚刚意识到我可以使用chrome.runtime.sendMessage从我的网页直接发送消息到应用程序,所以我猜这个扩展是不需要的。然而,我仍然需要应用程序一直在收听消息。

解决方案



我将所有逻辑移至清单文件中设置为后台脚本的脚本(background.js):

 app:{
background:{
scripts:[background.js]
}
),

在background.js中,我设置了所有(包括消息侦听器) chrome.app.runtime.onStartup.addListener(function(){


I'm trying to build a Chrome app and/or extension to communicate with a serial device (using https://developer.chrome.com/apps/serial), to use for showing data from the device on a webpage.

I've created an app that can read messages from my device but I now need to get those messages to my webpage. My plan was to create an extension that communicates with my app and then updates my webpage. My extension sends messages to the app using "chrome.runtime.sendMessage" and my app then responds with messages containing information from my device.

This works fine when the app is running but when I close my app it stops listening for new messages. Is there any way to always keep my app listening to new messages?

The reason I've created an app (and not just an extension) from the start is because extensions are not allowed to communicate with serial devices. Is there some other way than building both an app and an extension I can achieve what I want?

Update:
I just realized that I can send messages directly to the app from my webpage using "chrome.runtime.sendMessage", so I guess that the extension isn't needed. I however still need the app to be listening to messages all the time.

解决方案

Problem solved :)

I moved all the logic to the script (background.js) that was being set as the background script in the manifest file:

"app": {
    "background": {
      "scripts": [ "background.js" ]
    }
  },

In background.js I setup everything (including the message listener) on chrome.app.runtime.onStartup.addListener(function() { .

这篇关于让Chrome Packaged应用程序在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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