ASP.NET SignalR:无法加载资源

时间:2018-10-09 02:54:06

标签: .net signalr

我是signalR的新手。我一直在尝试使此代码正常工作。我在控制台中遇到错误。 error

我认为第二个错误是第一个错误的乘积。

我不确定这意味着什么或如何解决。我看了几本不同的教程,但我一直无法弄清楚。我尝试搜索互连网,但找不到。我显然对此没有深刻的了解,并且学会了最好弄脏我的手(可以这么说)。我希望有人可以帮忙。下面是我的代码

启动:

using Microsoft.Owin;
using Owin;
using SignalR_Test;

namespace SignalR_Test
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

集线器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalR_Test
{
    [HubName("employeeHub")]//the name through which in js file we init and call hub start fuction. 
    public class EmployeeHub : Hub
    {
        private readonly Employee _employee;
        public EmployeeHub() : this(Employee.Instance)
        {

        }
        public EmployeeHub(Employee stockTicker)
        {
            _employee = stockTicker;
        }
        [HubMethodName("getAllEmployee")]
        public IEnumerable<Emp> GetAllEmployee()
        {
            return _employee.GetEmployee();
        }
    }
}

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml Jump ">
<head>
    <title>SignalR Example</title>
</head>
<body>
    <script src="Scripts/jquery-1.6.4.js"></script>
    <script src="Scripts/jquery.signalR-2.3.0.js"></script>
    <script src="../signalr/hubs"></script>
    <!--generate a javascript proxy class for our server class-->
    <script>

        $(function () {
            var hub = $.connection.employeeHub;
            hub.client.getAllEmployee = function (eItems) {
                eItems.each(function (i) {
                    $("htmlCode") = "<tr>";
                    $("htmlCode") = $("htmlCode") + "<td>" + this.EmpId + "</td><td>" + this.EName + "</td><td>" + this.DeptNo + "</td>"
                    $("htmlCode") = "</tr>";
                    $("#code").html($("htmlCode"));
                });
            };
        });

    </script>
    <h1>SqlTableDependencly with SignalR</h1>
    <div class="col-sm-6">
        <table class="table table-bordered table-hover table-striped">
            <tr>
                <th>
                    EmpId
                </th>
                <th>
                    EName
                </th>
                <th>
                    DeptNo
                </th>
            </tr>
            <tbody>
                <span id="code"></span>
            </tbody>
        </table>
    </div>
</body>
</html>

web.config

<?xml version="1.0" encoding="utf-8"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
         <add name="sqlCon" connectionString="data source=RANCOR\HARDAC;Integrated Security=SSPI;initial catalog=signalR" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1"/>
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

根据以下建议对脚本部分进行了更改。我仍然遇到错误。错误:Failed to load resource: the server responded with a status of 404 (Not Found) 编辑index.html

   <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml Jump ">
<head>
    <title>SignalR Example</title>
</head>
<body>
    <script src="Scripts/jquery-1.6.4.js"></script>
    <script src="Scripts/jquery.signalR-2.3.0.js"></script>
    <script src="~/signalr/hubs"></script> <!-- ERROR happening here -->
    <!--generate a javascript proxy class for our server class-->
    <script>

        $(function () {
            var hub = $.connection.employeeHub;
            // If you like to call methodes on client from server. Do it here
            $.connection.hub.start().done(function () {
                // In the case the connection is started you call the method on the server
                hub.client.getAllEmployee = function (eItems) {
                    // Do your stuff here
                    $("htmlCode") = "<tr>";
                    $("htmlCode") = $("htmlCode") + "<td>" + this.EmpId + "</td><td>" + this.EName + "</td><td>" + this.DeptNo + "</td>"
                    $("htmlCode") = "</tr>";
                    $("#code").html($("htmlCode"));
                }
            });
        });
    </script>
    <h1>SqlTableDependencly with SignalR</h1>
    <div class="col-sm-6">
        <table class="table table-bordered table-hover table-striped">
            <tr>
                <th>
                    EmpId
                </th>
                <th>
                    EName
                </th>
                <th>
                    DeptNo
                </th>
            </tr>
            <tbody>
                <span id="code"></span>
            </tbody>
        </table>
    </div>
</body>
</html>

编辑: 回应评论 我尝试了各种变体以获得中枢js。 1. <script src="/signalr/hubs"></script> 2. <script src="signalr/hubs"></script> 3. <script src="~/signalr/hubs"></script> 4. <script src="../signalr/hubs"></script>

我还尝试将以下代码添加到Web.config文件中:

  <appSettings>
    <add key="owin:AutomaticAppStartup" value="true"></add>
  </appSettings>

1 个答案:

答案 0 :(得分:0)

我看到两个问题:

1。连接应该首先开始

2。如果您想在服务器上调用某些内容,则需要在服务器属性上而不是在客户端上进行操作

示例:

var hub = $.connection.employeeHub;
     // If you like to call methodes on client from server. Do it here
     $.connection.hub.start().done(function () {
       // In the case the connection is started you call the method on the server
        hub.client.getAllEmployee = function (eItems) {
        // Do your stuff here
        }
     });
});