简体   繁体   中英

SignalR Client Hub Proxy is Undefined

I was following this tutorial. On client side, in a simple html page, I get undefined for the client hub proxy from SignalR; What am I missing?

This links work properly (client is another asp.net mvc project at the same solution):

http://localhost:28538/Scripts/jquery.signalR-2.0.0.min.js
http://localhost:28538/Scripts/jquery-1.8.2.min.js
http://127.0.0.1:9077/signalr/hubs
http://127.0.0.1:9077/signalr/js

My Hub class:

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

Startup class (which will be passed to WebApp.Start ):

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

The main part of the app (a windows service, but that's irrelevant):

class MyAppSvc : WinSvc.ISvc
{
    IDisposable _app;
    string _url = "http://127.0.0.1:9077";

    public void OnShutdown()
    {
        _app.Dispose();
    }

    public void OnStart(string[] args)
    {
        _app = Microsoft.Owin.Hosting.WebApp.Start<MyApp.SigR.Startup>(_url);
    }

    public void OnStop()
    {
        _app.Dispose();
    }
}

The actual html page; the client:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>SigR Sample</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 src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR-2.0.0.min.js" type="text/javascript"></script>
    <script src="http://127.0.0.1:9077/signalr/hubs" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            //Set the hubs URL for the connection
            $.connection.hub.url = "http://127.0.0.1:9077/signalr";

            $.connection.hub.logging = true;

            // Declare a proxy to reference the hub.
            var chat = $.connection.alohaHub;
            alert(chat);
            // Create a function that the hub can call to broadcast messages.
            chat.client.addMessage = 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.start().done(function () {
                $('#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();
                });
            });
        });
    </script>
</body>
</html>

You need to enable CORS support on your server for cross domain to work (I'll also list how to enable jsonp).

To Enable Cors:

  1. Install Microsoft ASP.NET Cross-Origin Support via nuget (Microsoft.Owin.Cors)
  2. Add this to your startup file (before your map signalr call):

:

app.UseCors(CorsOptions.AllowAll); // You can modify the CorsOptions

To Enable JSONP:

Modify your "MapSignalR" in your startup file via:

app.MapSignalR(new HubConfiguration
{
    EnableJSONP = true
});

To do both together you can do:

app.UseCors(CorsOptions.AllowAll)
    .MapSignalR(new HubConfiguration
    {
        EnableJSONP = true
    });

Keep in mind enabling these cross domain features on your SignalR server exposes it to potential security vulnerabilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM