如何在 chrome 扩展中使用信号器

How to use signalr in chrome extensions

我正在尝试在我的 chrome 扩展中使用 signalr,但我一直收到

'Uncaught TypeError: Cannot read property 'client' of undefined'

现在我知道我需要使用脚本

<script src="http://localhost:3600/signalr/hubs"></script>

像这样,但我不知道在 chrome 扩展中在哪里以及如何使用它。

我的清单文件如下所示

"manifest_version": 2,    
  "name": "Jquery Tests",
  "description": "This extension tests jquery.",
  "version": "1.0",       
    "background": {
    "scripts": ["jquery-1.9.1.min.js", "jquery.signalR-2.2.1.min.js", "background.js"],
    "persistent": true
  },    
    "content_scripts":
    [
        {
            "matches": ["http://*/*", "https://*/*"],
            "js": ["jquery-1.9.1.min.js", "jquery.signalR-2.2.1.min.js", "popup.js"]
        }
    ],    

  "permissions": [
    "notifications",
    "background",
    "tabs", "http://*/*"
  ],    
  "browser_action": {
    "default_icon": "img/icon.png",
    "default_popup": "popup.html"
  }

这是我的 popup.js 我得到错误的地方

$(document).ready(function () {    
    debugger;
    console.log('jquery working in content!');
    $.connection.hub.url = "http://localhost:3360/signalr";
    $.connection.logging = true;
    var ticker = $.connection.notificationHub;
    //var ticker = $.connection.tempratureMonitorHub;

    //signalr method for push server message to client
    ticker.client.notify = function (message) {
        console.log("Notification added!");
        if (message && message.toLowerCase() == "added") {
            updateNotificationCount();
        }
    }
});
[][1]

在 chrome extensions 中,动态创建的代理即 hubs 文件不可用,因为每次安装都会加载一次文件。 此文件包含 clientserver 的信息。要在您的扩展中包含此信息,您需要按照 this link

中提到的以下步骤操作
  1. 安装 Microsoft.AspNet.SignalR.Utils NuGet 包。
  2. 打开命令提示符并浏览到包含以下内容的工具文件夹 SignalR.exe 文件。工具文件夹位于以下位置:

    [你的解决方案 文件夹]\packages\Microsoft.AspNet.SignalR.Utils.2.1.0\tools

  3. 输入以下命令:

    signalr ghp /path:[path to the .dll that contains your Hub class]
    
    The path to your .dll is typically the bin folder in your project
    folder.
    

    此命令在与以下命令相同的文件夹中创建一个名为 server.js 的文件 signalr.exe.

  4. server.js 文件放在项目的适当文件夹中, 根据您的应用程序重命名它,并添加一个参考 用它代替 "signalr/hubs" 参考。