23

このチュートリアルを新しいプロジェクトで動作させることはできますが、既存のプロジェクトでは動作しません。

私のプロジェクトは、web.config ファイルに次の属性を持つ ASP.Net MVC 4 Web アプリケーションです。

<appSettings>
  <add key="webpages:Enabled" value="true"/>
</appSettings>

これは、私のアプリケーションが、クライアント側で AngularJS を使用するシングル ページ アプリケーションであるためです。私のアプリケーションの唯一のページは index.cshtml で、ここに signalR の関連コードを追加しました。

 <!-- signalR chat -->
<script src="~/Scripts/jquery.signalR-1.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/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.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>

次に、ChatHub.cs ファイルを取得しました。

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

そして最後に global.asax で:

 protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

アプリケーションを実行すると、/signalr/hubs ファイルが生成されません。ファイルを要求すると 404 が返され、次の行でクラッシュします。

 chat.client.broadcastMessage = function (name, message) { ....

前の行で chatHub が見つからなかったため、chat が null であるためです。

var chat = $.connection.chatHub;

私のコードの何が問題なのか誰か知っていますか?

アップデート

行を変更して問題を解決しました::

<script src="/signalr/hubs"></script>

<script src="~/signalr/hubs"></script>
4

8 に答える 8

20

行を変更して問題を解決しました::

<script src="/signalr/hubs"></script>

<script src="~/signalr/hubs"></script>
于 2013-07-02T10:01:44.267 に答える
2

私の場合、ChatHub クラスが public とマークされていなかったことが原因でした。

于 2015-11-28T21:35:07.717 に答える
0

最初からスケールアウトを始める人へのアドバイス。私の場合、リモートクライアントを機能させることに取り組んでいましたが、そのことに気づきませんでした

A. チュートリアルの例では、using ステートメントに Web アプリ (サーバー) の起動がリストされています。その後、Web アプリは適切に破棄され、存在しなくなります。

using ステートメントを削除し、後で破棄できるように Web アプリへの参照を保持します。

B. クライアントはサーバーとは異なる URL を持っています。この例は、それらが同じ URL を持つことに依存しています。「/signalr/hubs」は、シグナルサーバーによって実行されるエンドポイントであり、サーバーが実装するすべてのハブのスクリプトを取得するためにシグナルクライアントによって呼び出されます。

クライアントに対して相対にするのではなく、 「 http://myServerURL/signalr/hubs」を含める必要があります。

嘘はありません。いくつかの魔法により、ソリューションは同僚のセットアップでとにかく機能したため、これは2週間しっかりとつまずきました。これにより、コンピューターの接続をブロックしていたはずの IIS 設定とファイアウォール設定と CORS 設定を探し続けました。可能な限り最後のスタック オーバーフローに関する質問をすべて拾い集めた結果、最終的な答えは、最初から Web アプリにハートビート モニターを実装するべきだったというものでした。

頑張ってください。うまくいけば、これで他の人が時間を節約できます。

于 2020-05-05T15:19:32.097 に答える
0

I'll like to add that the signalR Readme file have some note about this issue. And also if your signalR page is in a PartialView some script should be place in the master page.

Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR.

Upgrading from 1.x to 2.0
-------------------------
Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to 
upgrade your SignalR 1.x application to 2.0.

Mapping the Hubs connection
----------------------------
To enable SignalR in your application, create a class called Startup with the following:

using Microsoft.Owin;
using Owin;
using MyWebApplication;

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

Getting Started
---------------
See http://www.asp.net/signalr/overview/getting-started for more information on how to get started.

Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'?
--------------------------------------------------------------------------------------------
This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'.
Please make sure that the Hub route is registered before any other routes in your application.

In ASP.NET MVC 4 you can do the following:

      <script src="~/signalr/hubs"></script>

If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references:

    <script src="@Url.Content("~/signalr/hubs")"></script>

If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager 
using a app root relative path (starting with a '~/'):

    <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>

If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest 
patches installed for IIS and ASP.NET. 
于 2017-04-10T14:25:13.037 に答える