我遇到了Signal R的一个奇怪问题,并且几乎用尽了我在网上运行过的所有解决方案。在本地和我们的质量检查服务器上,Signal R都可以正常工作。调用Hub方法,并且Hub方法使用Client.Group([groupname])。method()很好地调用其客户端方法。但是,在我们的生产服务器上,有一种集线器方法正在尝试通过Client.Group调用客户端方法,而这些方法只是没有被调用。我已经直接从本地开发副本和QA服务器复制了代码,但是无论我做了什么,我仍然无法在生产服务器上正常工作。
根据以下问题,我已经确保我的方法在正确的位置:SignalR JS Client Methods Not Invoked,并且我通读了许多其他问题,以尝试找到可能的答案,但目前没有任何效果。
下面是涉及的中心方法,首先将用户分配给一个组...
public Task AwaitRequest(string driverRole)
{
var driverCode = Context.User.Identity.Name;
IDriverStatus status = DriverStatusServiceFactory.GetCurrent.GetByCode("Login");
IDriverRoles role = DriverRolesServiceFactory.GetCurrent.GetByCode(driverRole);
var driver = SimpleInjectorFactory.GetCurrentContainer.GetInstance<IDriverLog>();
driver.DriverId = driverCode;
driver.DriverStatus = status.DriverStatusId;
driver.LastUpdated = DateTime.UtcNow;
driver.DriverRoleId = role.DriverRoleId;
DriverLogServiceFactory.GetCurrent.InsertUpdate(driver);
return Groups.Add(Context.ConnectionId, driverCode.ToLower());
}
,然后是另一个集线器方法,该方法应使用以下函数中的Clients.Group()...行来调用客户端方法。
public void AssignRequest(string driverRole)
{
Guid.TryParse(_currentFacilityId, out Guid currentFacilityIdGuid);
string driverCode = Context.User.Identity.Name;
IDriverLog driverLog = DriverLogServiceFactory.GetCurrent.GetByCode(driverCode);
Guid requestId =
ShuttleDataServiceFactory.GetCurrent.OfferHotTruckRequest(driverCode, currentFacilityIdGuid, driverRole);
if (requestId != Guid.Empty)
{
IHotTruckRequest request = DapperCrudHelper.GetById<HotTruckRequest, Guid>(requestId);
Clients.Group(driverCode.ToLower()).help();
Clients.Group(driverCode.ToLower()).assignRequest(request.Id, request.Accepted, request.IsNew);
Lazy<IHubContext> hubLazy =
new Lazy<IHubContext>(() => GlobalHost.ConnectionManager.GetHubContext<HotTruckAdminHub>());
hubLazy.Value.Clients.Group(_currentFacilityShortName).update(request);
if (!Equals(driverLog.DriverStatus, (int) DriverStatusEnum.Offered) &&
!Equals(driverLog.HotTruckRequestId, request.Id))
{
string msg = $"Assigned hot truck request {request.Id} to driver {driverCode}";
CreateAuditLog("Assigned Load", "Hot Truck", request.Id.ToString(), "system", _currentFacilityId,
_currentCompanyId, msg);
}
}
}
下面是应该调用的两个客户端方法,但不是。
hotTruckHub.client.help = function() {
console.log("blah");
}
hotTruckHub.client.assignRequest = function (requestId, Accepted, isNew) {
console.log(requestId);
if (requestId !== 0) {
clearInterval(working);
$('#RequestId').val(requestId);
if (isNew) {
hideshow('AcceptLoad');
CurrentRequestId = requestId;
if (loadPendingAcceptance === null || loadPendingAcceptance === undefined) {
loadPendingAcceptance = setInterval(function() {
hotTruckHub.server.checkLoadStatus(requestId);
},
2000);
}
} else {
if (Accepted) {
clearInterval(loadPendingAcceptance);
clearInterval(working);
} else {
hideshow('AcceptLoad');
CurrentRequestId = requestId;
clearInterval(loadPendingAcceptance);
clearInterval(working);
}
}
if (accept === null || accept === undefined) {
accept = setInterval(function() {
hotTruckHub.server.declineLoad(CurrentRequestId, true);
},
180000);
}
}
};
我在Chrome的控制台中都没有收到任何错误消息。在本地和QA服务器上,这两种方法均被成功调用,我在控制台中触发了console.log(),否则获得了预期的功能。