SignalR 结合 WebApi
SignalR combined with WebApi
我正在对 SignalR 进行概念验证。基本上我遵循了这个网站的教程:Tutorial: Getting Started with SignalR 2 并且我正在尝试从另一个项目中的 WebApi 向前端发送消息。
javascript代码如下:
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
我的集线器代码:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
当我构建此代码时,它的工作方式与上面 link 中的示例一样。但是,当我尝试使用以下代码从另一个项目发送消息时,它不起作用:
public class MyMessageHandler : IHandleMessages<MyMessage>
{
static ILog log = LogManager.GetLogger<MyMessageHandler>();
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
log.Info($"Message received: {message.Name}");
var hub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
hub.Clients.All.Send("Admin", "stop the chat");
return Task.Delay(0);
}
}
当我 运行 这段代码时,前端 运行ning 在此 link/port: http://localhost:8854/index.html
和 webapi 在此 link/port: http://localhost:8387/api/values
.
我觉得我遗漏了最后一件事,但是无数 Google 结果帮不了我。有人知道我该如何解决这个问题吗?
正如我们从评论中概述的那样,您在服务器端有两个应用程序实例。
您的中心在一个实例中:public class ChatHub : Hub
。
这个调用在另一个:GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
.
所以第二个集线器实例不等于连接客户端的集线器实例。
要解决此问题,您需要为 signalr 配置 backplane。它允许在服务器的不同实例中使用相同的集线器。我建议使用 SQL 服务器选项而不是 Azure/Redis。
另一个解决方案是在第二个实例中停止使用服务器端集线器。并使用客户端 C# signalR 代码连接到第一个实例(和集线器)。
我正在对 SignalR 进行概念验证。基本上我遵循了这个网站的教程:Tutorial: Getting Started with SignalR 2 并且我正在尝试从另一个项目中的 WebApi 向前端发送消息。
javascript代码如下:
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
我的集线器代码:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
当我构建此代码时,它的工作方式与上面 link 中的示例一样。但是,当我尝试使用以下代码从另一个项目发送消息时,它不起作用:
public class MyMessageHandler : IHandleMessages<MyMessage>
{
static ILog log = LogManager.GetLogger<MyMessageHandler>();
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
log.Info($"Message received: {message.Name}");
var hub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
hub.Clients.All.Send("Admin", "stop the chat");
return Task.Delay(0);
}
}
当我 运行 这段代码时,前端 运行ning 在此 link/port: http://localhost:8854/index.html
和 webapi 在此 link/port: http://localhost:8387/api/values
.
我觉得我遗漏了最后一件事,但是无数 Google 结果帮不了我。有人知道我该如何解决这个问题吗?
正如我们从评论中概述的那样,您在服务器端有两个应用程序实例。
您的中心在一个实例中:public class ChatHub : Hub
。
这个调用在另一个:GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
.
所以第二个集线器实例不等于连接客户端的集线器实例。
要解决此问题,您需要为 signalr 配置 backplane。它允许在服务器的不同实例中使用相同的集线器。我建议使用 SQL 服务器选项而不是 Azure/Redis。
另一个解决方案是在第二个实例中停止使用服务器端集线器。并使用客户端 C# signalR 代码连接到第一个实例(和集线器)。