如何在chrome扩展中使用signalr

时间:2016-10-15 05:31:45

标签: google-chrome-extension signalr signalr-hub

我试图在我的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]

1 个答案:

答案 0 :(得分:1)

在Chrome扩展程序中,动态创建的代理,即集线器文件不可用,因为每次安装都会加载一次文件。 此文件包含客户端服务器的信息。要在扩展程序中包含此信息,您需要按照this link

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

    [你的解决方案 文件夹] \包\ Microsoft.AspNet.SignalR.Utils.2.1.0 \工具

  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 文件放入项目的相应文件夹中,     根据您的应用程序重命名它,并添加引用     代替&#34;信号器/集线器&#34;参考

相关问题