SignalR js 2 示例未在 2019 年调用

SignalR js 2 example not Invoked in 2019

我无法获取 SignalR 服务器端 Hub 代码来调用 JS 客户端 methods.i 收到此错误消息 无法读取未定义的 属性 'client' 并且找不到此文件夹http://localhost:8087/signalr/hubs . I've been fairly careful to avoid obvious traps but I guess I'm still overlooking something and i follow the steps as it is from this link https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr。这是我的代码:

这个 ChatHub class:

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

第 html 页:

<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.10.2.js"></script>
<script src="Scripts/jquery.signalR-2.4.1.min.js"></script>

<script src="signalr/hubs"></script>
<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 () {
              console.log( 'Connection established!' );
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.SendMessage($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
</script>

这是 Startup.cs 文件:

using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

[assembly: OwinStartup(typeof(Consultation_WebServices.Startup))]
namespace Consultation_WebServices {
 public class Startup {
  public void Configuration(IAppBuilder app) {
   app.MapSignalR();
  }
 }
}

确保将您的 Hub class 移出 App_Code 文件夹。

有关详细信息,您可以阅读 SignalR 存储库中的以下内容issue