我有一个旧版Webform项目,在该项目中需要集成Signalr Technology我知道信号器/集线器是动态生成的,但是在我的特定项目中我无法使其工作。我试图加载所有网址路径,例如:
<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>
这很好,因为我的客户端可以与我的chatHub(serverside)建立连接,如下所示初始化我的聊天变量:
但是,如下图所示,我在服务器到客户端之间的协商过程中出现了错误:
[我认为是因为当服务器尝试与客户端通信时,它会以[signalr / hub]的形式生成url,但是正如我之前提到的,我正在使用/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>
您可以检查是否在startup.cs的配置功能中添加了端点
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapHub<ChartHub>("/charthub");
});
这是我在项目中使用的。