SignalR客户端集线器代理未定义 [英] SignalR Client Hub Proxy is Undefined

查看:127
本文介绍了SignalR客户端集线器代理未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注教程.在客户端,在一个简单的html页面中,我从SignalR获得了未定义的客户端中心代理;我想念什么?

I was following this tutorial. On client side, in a simple html page, I get undefined for the client hub proxy from SignalR; What am I missing?

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

This links work properly (client is another asp.net mvc project at the same solution):

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服务,但这无关紧要):

The main part of the app (a windows service, but that's irrelevant):

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页面;客户:

The actual html page; the client:

<!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>

推荐答案

您需要在服务器上启用CORS支持,以便跨域工作(我还将列出如何启用jsonp).

You need to enable CORS support on your server for cross domain to work (I'll also list how to enable jsonp).

要启用Cors:

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

:

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

启用JSONP:

通过以下方法在启动文件中修改"MapSignalR":

Modify your "MapSignalR" in your startup file via:

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

您可以同时做这两个事情:

To do both together you can do:

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

请记住,在SignalR服务器上启用这些跨域功能会将其暴露于潜在的安全漏洞中.

Keep in mind enabling these cross domain features on your SignalR server exposes it to potential security vulnerabilities.

这篇关于SignalR客户端集线器代理未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆