0

I have created the simple chat hub tutorial from the asp.net/signalr site.

I have modified it to run from a different domain eg: realtime.mydomain.com

I have installed the package: Install-Package Microsoft.Owin.Cors

Everything is working with Web Sockets on IE10 and Chrome, Firefox cross domain.

I can't get IE9 to work when running cross domain (works only if running from same domain).


Here is the code on 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);
        }

    }
}

Here is the code on 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 always fails to connect when run cross site.

George Filippakos
  • 16,359
  • 15
  • 81
  • 92
  • refer this question http://stackoverflow.com/questions/9984534/signalr-cross-domain-connections-cors-access-control-allow-origin-issues – swapneel Nov 22 '13 at 13:03

2 Answers2

0

Seem to work when set:

var hubConfiguration = new HubConfiguration
{
     EnableJSONP = true
};
George Filippakos
  • 16,359
  • 15
  • 81
  • 92
0

It happens because IE9, 8 and so on, don't support CORS (or support partially).

Take a look here to have a list. In that case you can use JSOP (be careful because it uses querystring so long message could not be sent).

.u

imperugo
  • 385
  • 1
  • 9