【发布时间】: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