【问题标题】:SignalR Client Hub Proxy is UndefinedSignalR 客户端集线器代理未定义
【发布时间】:2013-10-29 22:29:01
【问题描述】:

我正在关注this 教程。在客户端,在一个简单的 html 页面中,我从 SignalR 获得未定义的客户端集线器代理;我错过了什么?

此链接正常工作(客户端是同一解决方案中的另一个 asp.net mvc 项目):

http://localhost:28538/Scripts/jquery.signalR-2.0.0.min.js
http://localhost:28538/Scripts/jquery-1.8.2.min.js
http://127.0.0.1:9077/signalr/hubs
http://127.0.0.1:9077/signalr/js

我的 Hub 类:

class AlohaHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }
}

启动类(将传递给WebApp.Start):

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

应用程序的主要部分(Windows 服务,但这无关紧要):

class MyAppSvc : WinSvc.ISvc
{
    IDisposable _app;
    string _url = "http://127.0.0.1:9077";

    public void OnShutdown()
    {
        _app.Dispose();
    }

    public void OnStart(string[] args)
    {
        _app = Microsoft.Owin.Hosting.WebApp.Start<MyApp.SigR.Startup>(_url);
    }

    public void OnStop()
    {
        _app.Dispose();
    }
}

实际的html页面;客户:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>SigR Sample</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
    <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR-2.0.0.min.js" type="text/javascript"></script>
    <script src="http://127.0.0.1:9077/signalr/hubs" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            //Set the hubs URL for the connection
            $.connection.hub.url = "http://127.0.0.1:9077/signalr";

            $.connection.hub.logging = true;

            // Declare a proxy to reference the hub.
            var chat = $.connection.alohaHub;
            alert(chat);
            // Create a function that the hub can call to broadcast messages.
            chat.client.addMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
    </script>
</body>
</html>

【问题讨论】:

  • 我猜这是由于跨域问题...您是否在同一个 localhost 端口上托管 SignalR?
  • 否;端口不同。所以我在 web.config 中添加了&lt;httpProtocol&gt; &lt;customHeaders&gt; &lt;add name="Access-Control-Allow-Origin" value="*" /&gt; &lt;add name="Access-Control-Allow-Headers" value="Content-Type" /&gt; &lt;/customHeaders&gt; &lt;/httpProtocol&gt;;但没有成功(或者我对这个话题一无所知)。

标签: c# javascript jquery asp.net-mvc signalr


【解决方案1】:

您需要在服务器上启用 CORS 支持才能跨域工作(我还将列出如何启用 jsonp)。

启用 Cors:

  1. 通过 nuget (Microsoft.Owin.Cors) 安装 Microsoft ASP.NET 跨域支持
  2. 将此添加到您的启动文件中(在您的地图信号器调用之前):

app.UseCors(CorsOptions.AllowAll); // You can modify the CorsOptions

启用 JSONP:

通过以下方式修改启动文件中的“MapSignalR”:

app.MapSignalR(new HubConfiguration
{
    EnableJSONP = true
});

要同时做这两件事,你可以这样做:

app.UseCors(CorsOptions.AllowAll)
    .MapSignalR(new HubConfiguration
    {
        EnableJSONP = true
    });

请记住,在您的 SignalR 服务器上启用这些跨域功能会使其面临潜在的安全漏洞。

【讨论】:

  • 我必须安装 Microsoft.Owin.Cors,而不是 Microsoft.AspNet.Cors(我认为这是 SignalR 2.x 的东西)
猜你喜欢
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2019-09-13
  • 2012-09-06
  • 2019-10-07
  • 1970-01-01
相关资源
最近更新 更多