简体   繁体   English

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

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

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. 问题是它永远不会到达Connection成功或失败的警报,并且alert(chat)调用返回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 谢谢你,杰森

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 . 手动创建集线器文件并将其托管在当前域中: 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. 使用原始集线器连接API,根本不包括signalr / hubs文件。

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 (第二个代码段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM