【问题标题】:SignalR v2 not working in <IE10 Cross DomainSignalR v2 在 <IE10 跨域中不工作
【发布时间】:2013-11-22 21:32:01
【问题描述】:

我从asp.net/signalr 站点创建了简单的聊天中心教程。

我已将其修改为从不同的域运行,例如:realtime.mydomain.com

我已经安装了包:Install-Package Microsoft.Owin.Cors

一切都在 IE10 和 Chrome、Firefox 跨域上使用 Web Sockets。

我无法让 IE9 在跨域运行时工作(仅在从同一域运行时才有效)。


这是 realtime.mydomain.com 上的代码

Startup.cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;

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

namespace SignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {

            // Branch the pipeline here for requests that start with "/signalr"
            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);

                var hubConfiguration = new HubConfiguration
                {
                    //EnableJSONP = true
                };

                map.RunSignalR(hubConfiguration);
            });

        }
    }
}

ChatHub.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace SignalR
{
    public class ChatHub : Hub
    {

        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }

    }
}

这是 www.mydomain.com 上的代码

index.html

<!DOCTYPE html>
<html>
<head>
    <title>SignalR Simple Chat</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 references. -->
    <!--Reference the jQuery library. -->
    <script src="http://realtime.mydomain.com/scripts/jquery-1.10.2.min.js"></script>
    <!--Reference the SignalR library. -->
    <script src="http://realtime.mydomain.com/scripts/jquery.signalR-2.0.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="http://realtime.mydomain.com/signalr/hubs"></script>

    <!--Add script to update the page and send messages.-->
    <script type="text/javascript">
        $(function () {

            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = 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.url = 'http://realtime.mydomain.com/signalr';
            //jQuery.support.cors = true; <-- Do not enable - SignalR handles the use of CORS automatically 
            $.connection.hub.start().done(function () {
                console.log("Connected, transport = " + $.connection.hub.transport.name);

                $('#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();
                });

            }).fail(function () {
                alert("failed to connect!");
            });

        });
    </script>
</body>
</html>

IE9跨站运行时总是连接不上。

【问题讨论】:

标签: .net signalr


【解决方案1】:

设置后似乎可以工作:

var hubConfiguration = new HubConfiguration
{
     EnableJSONP = true
};

【讨论】:

    【解决方案2】:

    这是因为IE9、8等不支持CORS(或部分支持)。

    查看here 以获取列表。 在这种情况下,您可以使用 JSOP(要小心,因为它使用查询字符串,因此无法发送长消息)。

    .u

    【讨论】:

      猜你喜欢
      • 2013-02-09
      • 2014-10-04
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2013-08-30
      • 2023-03-17
      • 1970-01-01
      • 2013-05-28
      相关资源
      最近更新 更多