SignalR客户端中心代理未定义 - c#

我正在遵循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>

参考方案

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

启用Cors:

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

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服务器上启用这些跨域功能会使其暴露于潜在的安全漏洞。

使用php重新加载内容 - javascript

在对网站进行编程时,我以前使用过此代码,它可以完美工作,但是现在当我想使用一些Flash部件时,每次单击链接时,它都会重新加载所有网站。源代码: <!DOCTYPE html> <html> <head> <title>Hot King Staff</title> <meta charset=…

AJAX调用只能与$(document).on('click')一起使用 - php

我有一个显示数据库条目的表。用户能够为每一行打开一个弹出菜单。选项之一是删除数据库条目,并且该表应通过AJAX调用相应地刷新。只要有人单击#delete-toggle中的table-popup,我就会在HTML页面上进行AJAX调用(table-popup是div,当有人单击每行中存在的表中的table-edit-button时出现的表): <div …

用jQuery填充模式形式 - javascript

我正在将订单表从数据库绘制到datatables(jquery插件)中。我要在每笔最后一笔交易或每笔交易中增加付款。问题是,如何获取单击添加付款按钮以添加付款的行的订单ID。其次,当点击addpayment时,它会弹出一个带有字段或订单号的模态表单。我想用在td中找到的订单ID填充该字段,并使其不可编辑或隐藏,但在提交模态表单时将其发布到服务器。表格和模式表…

通过电子邮件发送库存状态表 - javascript

                        我正在寻找一种通过电子邮件发送具有相同HTML结果的产品确认页(带有可用输入)的解决方案。我对从mail.php的确认页面获取输入不感到困惑。请在下面找到代码。谢谢stockdetailform.php<html> <head> <title>Stockr Form</…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…