javascript 如何让 SignalR 集线器连接跨域工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16875228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:18:51  来源:igfitidea点击:

How do I get a SignalR hub connection to work cross-domain?

javascriptcross-domainsignalr

提问by Jason

I am trying to get a super simple hub connection working cross-domain but having no luck. I've read dozens of posts and done everything mentioned but still no success.

我试图让一个超级简单的集线器连接跨域工作,但没有运气。我已经阅读了数十篇帖子并完成了提到的所有内容,但仍然没有成功。

My server hub is here

我的服务器集线器在这里

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

My server MapHubs call is here

我的服务器 MapHubs 电话在这里

RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });

Any my javascript client is here

我的任何 javascript 客户端都在这里

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-2.0.1.min.js"></script>
    <script src="~/Scripts/jquery.signalR-1.1.2.min.js"></script>
    <script src="/signalr/hubs"></script>
</head>
<body>
    <div class="container">
        <input type="text" id="displayname" value="Test" />
        <input type="text" id="message" value="I'm here" />
        <input type="button" id="sendmessage" value="Send" />
    </div>
    <script type="text/javascript">
        $(function ()
        {
            $.connection.hub.url = 'http://<my url>/';
            var chat = $.connection.chatHub;
            alert(chat);
            $.connection.hub.start().done(function ()
            {
                alert("Connection succeeded");
            }).fail(function ()
            {
                alert("Connection failed");
            });
        });
    </script>
</body>
</html>

The problem is that it never reaches the Connection succeeded or failed alerts and the alert(chat) call returns undefined.

问题是它永远不会到达连接成功或失败的警报,并且警报(聊天)调用返回未定义。

I've tried several combinations for the $.connection.hub.url line

我已经为 $.connection.hub.url 行尝试了几种组合

$.connection.hub.url = 'http://<My url>';
$.connection.hub.url = 'http://<My url>/';
$.connection.hub.url = 'http://<My url>/signalr';
$.connection.hub.url = 'http://<My url>/signalr/';

The developer console in Chrome and Firebug give me the error

Chrome 和 Firebug 中的开发者控制台给了我错误

Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>. 

On the same domain it works fine. This is really starting to drive me crazy so any help would be appreciated.

在同一个域上它工作正常。这真的开始让我发疯,所以任何帮助将不胜感激。

Thanks, Jason

谢谢,杰森

采纳答案by N. Taylor Mullen

Your server is being hosted cross domain yet you're trying to get the hubs from the current domain. Therefore it's failing to retrieve the hubs file and you don't actually have a proxy to work with (which is why everything is not working).

您的服务器正在跨域托管,但您正在尝试从当前域获取集线器。因此,它无法检索集线器文件,并且您实际上没有可以使用的代理(这就是为什么一切都不起作用的原因)。

So you have two options:

所以你有两个选择:

  1. Manually create the hubs file and host it on the current domain: http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#manualproxy.
  2. Use the raw hub connection API and do not include the signalr/hubs file at all.
  1. 手动创建集线器文件并将其托管在当前域中:http: //www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#manualproxy
  2. 使用原始集线器连接 API,根本不包含信号器/集线器文件。

Here's a code snippet of how you can use the raw hub connection API: http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#nogenconnection(second code snippet).

以下是如何使用原始集线器连接 API 的代码片段:http: //www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#nogenconnection(第二个代码片段)。