0

私はこのチュートリアルに従っていました。クライアント側では、単純な html ページで、SignalR からのクライアント ハブ プロキシに対して undefined を取得します。私は何が欠けていますか?

このリンクは正常に機能します (クライアントは同じソリューションの別の asp.net mvc プロジェクトです)。

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

私のハブクラス:

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

スタートアップ クラス ( に渡されますWebApp.Start):

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

アプリの主要部分 (Windows サービスですが、それは関係ありません):

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

実際の html ページ。クライアント:

<!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>
4

1 に答える 1

4

クロスドメインを機能させるには、サーバーで CORS サポートを有効にする必要があります (jsonp を有効にする方法もリストします)。

Cors を有効にするには:

  1. nuget (Microsoft.Owin.Cors) を介して Microsoft ASP.NET Cross-Origin Support をインストールします。
  2. これをスタートアップ ファイルに追加します (マップ シグナルを呼び出す前に):

:

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

JSONP を有効にするには:

次の方法で、スタートアップ ファイルの「MapSignalR」を変更します。

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

両方を一緒に行うには、次のことができます。

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

SignalR サーバーでこれらのクロス ドメイン機能を有効にすると、潜在的なセキュリティの脆弱性にさらされることに注意してください。

于 2013-10-21T18:18:39.127 に答える