如何获得SignalR集线器连接跨域工作? [英] How do I get a SignalR hub connection to work cross-domain?

查看:466
本文介绍了如何获得SignalR集线器连接跨域工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个超级简单的集线器连接跨域工作,但没有运气。我已阅读了几十篇帖子,并完成了所有提到但仍未成功。

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.

我的服务器中心在这里

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

我的服务器MapHubs调用在这里

My server MapHubs call is here

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

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

Any my javascript client is here

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

问题是它从未到达连接成功或失败的警报,警报undefined。

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

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

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

$.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/';

Chrome和Firebug中的开发人员控制台会显示错误

The developer console in Chrome and Firebug give me the error

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.

感谢,
Jason

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. 手动创建hubs文件并将其托管在当前域中:http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client# manualproxy

  2. 使用原始中枢连接API,而不包含signalr / hubs档案。

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

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).

这篇关于如何获得SignalR集线器连接跨域工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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