1

我通过引用自行托管了一个 SignalR 服务器

未调用 Signalr Owin 简单示例 javascript 客户端”和“ https://github.com/SignalR/SignalR/wiki/Self-host

链接,但是当我尝试从 javascript 调用该集线器时,出现以下错误

“错误:SignalR:加载集线器时出错。确保您的集线器引用正确,例如。”

我的自托管服务器如下所示:

集线器类

using Microsoft.AspNet.SignalR; 
using System; 
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using Owin; 
namespace SignalrWorker { 
  public class Chat:Hub
    {
       public void Send()
       {
           Clients.All.send("Hi");
       }
    } 
  }

创业班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Owin;

namespace SignalrWorker {
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Turn cross domain on
            var config = new HubConfiguration { EnableCrossDomain = true ,EnableJavaScriptProxies=true,EnableDetailedErrors=true};

            // This will map out to http://localhost:8080/signalr by default
            app.MapHubs(config);
        }
    } 
}

Azure 辅助角色

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Net; 
using System.Threading; 
using Microsoft.WindowsAzure; 
using Microsoft.WindowsAzure.Diagnostics; 
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage; 
using Microsoft.AspNet.SignalR;
using Owin; 
using Microsoft.Owin.Hosting;

namespace SignalrWorker {
    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("SignalrWorker entry point called", "Information");
            try
            {


                using (WebApp.Start<Startup>("http://127.0.0.1:82/"))
                {
                    Trace.TraceInformation("Working", "Server running at http://127.0.0.1:82/");
                }


                while (true)
                {
                    Thread.Sleep(10000);
                    Trace.TraceInformation("Working", "Information");

                }
            }
            catch (Exception ex)
            {
                Thread.Sleep(1000);
                Trace.WriteLine("Error", ex.Message);
            }
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }
    } 
}

SignalR客户端如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
      @*  @Scripts.Render("~/bundles/modernizr")*@

    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>
                        <ul id="menu">
                            <li>@Html.ActionLink("Home", "Index", "Home")</li>
                            <li>@Html.ActionLink("About", "About", "Home")</li>
                            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>

        @Scripts.Render("~/bundles/jquery")
     <script src="~/Scripts/jquery.signalR-1.1.2.js"></script>
         <script type="text/javascript" src="http://127.0.0.1:82/signalr/hubs"></script>
        <script type="text/javascript">
            $(function () {


               /* // Proxy created on the fly
                var myHub = $.connection.chat;

                // Declare a function on the hub so the server can invoke it
                myHub.send = function (message) {
                    console.log("hi");
                };
                */
                // Start the connection
                $.connection.hub.url = 'http://127.0.0.1:82/signalr';
                $.connection.hub.start(function() {
                    console.log("hi");
                });


            });
        </script>
    </body>
</html>

由于工作陷入困境,请尽快提供帮助

提前致谢

4

0 回答 0