【问题标题】:SignalR Webform - signalr/hubs Negociate issueSignalR Webform - 信号器/集线器协商问题
【发布时间】:2020-02-19 14:39:59
【问题描述】:

我有一个遗留 Web 表单项目,需要在其中集成 Signalr 技术 我知道信号器/集线器是动态生成的,但是在我的特定项目中,我无法让它工作。

我尝试加载所有 url 路径,例如:

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


为了解决这个问题,我在另一个运行良好的项目中生成了 signalR,然后我将其复制到了我的脚本文件夹 /Scripts/SignalR/Hub.js 并在客户端引用了它。
<script src="../Scripts/SignalR/Hubs.js"></script>

它工作得很好,因为我的客户端可以与我的聊天中心(服务器端)建立连接,初始化我的聊天变量,如下所示:


但是在那之后我在服务器到客户端之间的协商过程中遇到了错误,如下图所示:



我认为是因为当服务器尝试与客户端通信时,它会将 url 生成为 [signalr/hub],但正如我之前提到的,我使用的是 /Scripts/SignalR/Hub.js 文件以实例断开与集线器的连接。

如何解决服务器与客户端之间的协商问题?

这是我的结构文件

startup.cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;
using SistemaJaspion.AppCode;
using System.Web.Routing;

[assembly: OwinStartup(typeof(SistemaJaspion.AppCode.Startup))]

namespace SistemaJaspion.AppCode
{
    /// <summary>
    /// The server needs to know which URL to intercept and direct to SignalR. To do that we add an OWIN startup class.
    /// </summary>
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            //app.MapSignalR("/signalr", new HubConfiguration());
            //app.MapSignalR();

            var hubConfiguration = new HubConfiguration();
            hubConfiguration.EnableDetailedErrors = true;
            hubConfiguration.EnableJavaScriptProxies = true;

            app.MapSignalR("/signalr", hubConfiguration);
        }

    }
}

ChatHub.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.Web;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace SistemaJaspion.AppCode
{
    /// <summary>
    /// The Hub class is used to define methods the clients can call on the server
    /// </summary>
    /// 

    [HubName("chatHub")]

    //[Microsoft.AspNet.SignalR.Authorize]
    public class ChatHub : Hub
    {


        static List<UsuariosSignalR> ConnectedUsers = new List<UsuariosSignalR>();


        // Number of connected clients
        private static int connectionCount = 0;


        public void Send(string name, string message)
        {
            // Append the current date
            name = DateTime.Now.ToString() + " " + name;
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }

        public override Task OnConnected()
        {

            // Increase the number of connections
            Interlocked.Increment(ref connectionCount);

            // Update the number of the connected users
            Clients.All.reportConnections(connectionCount);
            return base.OnConnected();
        }


        public override Task OnDisconnected(bool stopCalled)
        {
            // Decrease the number of connections
            Interlocked.Decrement(ref connectionCount);

            // Update the number of the connected users
            Clients.All.reportConnections(connectionCount);
            return base.OnDisconnected(stopCalled);
        }   
    }
}

MasterPage.aspx


    <script src="../Scripts/jquery-1.6.4.min.js"></script>
    <script src="../Scripts/jquery.signalR-2.4.1.js"></script>
    <script src="../Scripts/SignalR/Hubs.js"></script>


    <!--Add script to update the page and send messages.-->
    <script type="text/javascript">


         $(function () {
                    /* Peer-to-peer broadcast code */
                    // Declare a proxy to reference the ChatHub.

             debugger;

                   var chat = $.connection.chatHub;

                    chat.client.reportConnections = function (count) {
                        $("#usersCount").text(count);
                    }

                    $.connection.hub.logging = true;
                    $.connection.hub.start();
                });

    </script>

【问题讨论】:

    标签: c# signalr signalr-hub


    【解决方案1】:

    可以在startup.cs的configure函数中检查是否添加了endpoint

    app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                    endpoints.MapRazorPages();
    
                    endpoints.MapHub<ChartHub>("/charthub");
                });
    

    这是我在项目中使用的。

    【讨论】:

    • 你的类型项目是webform吗?
    • 我的 Visual Studio 版本的 asp .net core 中没有 Webform 类型的项目。您使用的是哪个 Visual Studio 版本?
    • VS 2017 - 我的项目基于 webforms
    • 你是对的@Jongkwan Park。我的项目基于使用 webforms 的网站项目类型。
    【解决方案2】:

    我明白为什么它不起作用了。

    首先,我对 signalR(我研究的示例)的研究是基于 WebApplication/webforms 项目,而在我的情况下,我是基于 Wesite/webforms 项目,它们之间的结构略有不同。

    无论如何,功劳归于Can SignalR be used with asp.net WebForms?,按照这些步骤我可以在网站/webform .net 项目上运行 signalR。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      相关资源
      最近更新 更多