我正在使用MassRnsit请求和SignalR响应。该网站向创建文件的Windows服务发出请求。创建文件后,Windows服务将向网站发送响应消息。该网站将打开该文件并使其可供用户查看。我想处理用户在创建文件之前关闭网页的场景。在这种情况下,我希望通过电子邮件将创建的文件发送给他们。
无论用户是否关闭了网页,都将运行响应消息的消息处理程序。我希望能够做的是在响应消息处理程序中知道网页已被关闭。这就是我已经做过的事情。它不起作用,但它确实说明了我的想法。在我的网页上
$(window).unload(function () {
if (event.clientY < 0) {
// $.connection.hub.stop();
$.connection.exportcreate.setIsDisconnected();
}
});
exportcreate是我的Hub名称。在setIsDisconnected中我会在Caller上设置一个属性吗?假设我成功设置了一个属性以指示网页已关闭。如何在响应消息处理程序中找到该值。这就是它现在所做的
protected void BasicResponseHandler(BasicResponse message)
{
string groupName = CorrelationIdGroupName(message.CorrelationId);
GetClients()[groupName].display(message.ExportGuid);
}
private static dynamic GetClients()
{
return AspNetHost.DependencyResolver.Resolve<IConnectionManager>().GetClients<ExportCreateHub>();
}
我正在使用邮件关联ID作为一个组。现在对我而言,关于消息的ExportGuid非常重要。那用于识别文件。因此,如果我要通过电子邮件发送创建的文件,我必须在响应处理程序中执行此操作,因为我需要ExportGuid值。如果我在关闭网页的集线器中将值存储在Caller上,我将如何在响应处理程序中访问它。
以防你需要知道。 display在网页上定义为
exportCreate.display = function (guid) {
setTimeout(function () {
top.location.href = 'GetExport.ashx?guid=' + guid;
}, 500);
};
GetExport.ashx打开文件并将其作为回复返回。
谢谢,
关心Ben
答案 0 :(得分:3)
我认为更好的选择是实现正确的连接处理。具体来说,让您的集线器实现IDisconnect和IConnected。然后,您将获得connectionId到文档Guid的映射。
public Task Connect()
{
connectionManager.MapConnectionToUser(Context.ConnectionId, Context.User.Name);
}
public Task Disconnect()
{
var connectionId = Context.ConnectionId;
var docId = connectionManager.LookupDocumentId(connectionId);
if (docId != Guid.Empty)
{
var userName = connectionManager.GetUserFromConnectionId(connectionId);
var user = userRepository.GetUserByUserName(userName);
bus.Publish( new EmailDocumentToUserCommand(docId, user.Email));
}
}
// Call from client
public void GenerateDocument(ClientParameters docParameters)
{
var docId = Guid.NewGuid();
connectionManager.MapDocumentIdToConnection(Context.ConnectionId, docId);
var command = new CreateDocumentCommand(docParameters);
command.Correlationid = docId;
bus.Publish(command);
Caller.creatingDocument(docId);
}
// Acknowledge you got the doc.
// Call this from the display method on the client.
// If this is not called, the disconnect method will handle sending
// by email.
public void Ack(Guid docId)
{
connectionManager.UnmapDocumentFromConnectionId(connectionId, docId);
Caller.sendMessage("ok");
}
当然这是我的头脑。