SignalR v2无法正常工作

时间:2013-11-22 12:48:25

标签: .net signalr

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

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

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

所有内容都适用于IE10和Chrome,Firefox跨域的Web套接字。

运行跨域时无法使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在跨站点运行时始终无法连接。

2 个答案:

答案 0 :(得分:0)

设置时似乎工作:

var hubConfiguration = new HubConfiguration
{
     EnableJSONP = true
};

答案 1 :(得分:0)

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

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

.U