如何使用SignalR与跨域 [英] How to use SignalR with cross domain

查看:397
本文介绍了如何使用SignalR与跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有跨域的SignalR但是在调用start函数时我收到错误消息。错误消息是未捕获的TypeError:无法调用方法'未定义'的开始'

I am trying to use SignalR with cross domain but i am getting error message when calling start function. Error message is "Uncaught TypeError: Cannot call method 'start' of undefined "

我正在使用代码
服务器端:

I am using code Server side:

[assembly: OwinStartup(typeof(SignalRChat.Startup))]

    namespace SignalRChat
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.Map("/signalr", map =>
                {              
                    map.UseCors(CorsOptions.AllowAll);
                    var hubConfiguration = new HubConfiguration
                    {                   
                       EnableJSONP = true
                    };               
                    map.RunSignalR(hubConfiguration);
                });
            }
        }
    }

客户端代码。

 <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="/Scripts/jquery-1.6.4.min.js"></script>   
        <script src="/Scripts/jquery.signalR-1.1.4.min.js"></script>
    </head>
    <body>
       <div></div>
       <script type="text/javascript">
        var connection = $.connection.hub.url ='http://localhost:9370/signalr';      
        connection.hub.start()
          .done(function () {
              alert('Now connected, connection ID=' + connection.id);
          });
      </script>
      </body>
      </html>


推荐答案

信号器的初始化和启动存在问题连接,也声明代理引用集线器。见下面的例子:

There are problems with the initialization and start of your Signalr connection, also declare a proxy to reference the hub. See below example:

<script src="/Scripts/jquery-1.6.4.min.js"></script>   
<script src="/Scripts/jquery.signalR-1.1.4.min.js"></script>
<script src="http://localhost:9370/signalr/hubs"></script>

 <script type="text/javascript">
    $.connection.hub.url ='http://localhost:9370/signalr';
    var yourHubProxy = $.connection.YourHubName;

    //Do something here with yourHubProxy

    $.connection.hub.start().done(function () {
        alert('Now connected, connection ID=' + $.connection.hub.id);
     });
 </script>

另一件事,我不确定你为什么要使用不同版本的服务器端和客户端的SignalR 。对我来说,你的服务器端有 SignalR 2.x ,你的客户端有 SignalR 1.1.4

Another thing, I'm not sure why you're using different versions of SignalR in your server side and client side. To me you had SignalR 2.x on your server side and SignalR 1.1.4 on your cient side.

看看以下链接,这是一个关于带有跨域的SignalR的好例子。
http ://damienbod.wordpress.com/2013/11/01/signalr-messaging-with-console-server-and-client-web-client-wpf-client/

Take a look at the following link, it's good an example about SignalR with cross domain. http://damienbod.wordpress.com/2013/11/01/signalr-messaging-with-console-server-and-client-web-client-wpf-client/

这篇关于如何使用SignalR与跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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