저는 SignalR의 개념 증명을하고 있습니다. 기본적으로 나는이 웹 사이트에서 튜토리얼을 따라 갔다 : https://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr 그리고 다른 프 로젝트의 WebApi에서 프론트 엔드로 메시지를 보내려고한다.WebApi와 결합 된 SignalR
자바 스크립트 코드는 다음과 같습니다 내 허브에 대한
$(function() {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
console.log("stuff");
// 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>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// 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);
}
}
내가이 그냥 위의 링크의 예처럼 작동 코드 구축
. 내가 다음 코드를 사용하여 다른 프로젝트에서 메시지를 보내려고하지만 때, 그것은 작동하지 않습니다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);
}
}
나는 프론트 엔드이 링크/포트에서 실행되는이 코드를 실행하면이 링크를 http://localhost:8854/index.html과 webapi을/포트 : http://localhost:8387/api/values
내가 마지막으로 일을 놓친 거지 같은 느낌,하지만 Google 검색 결과의 수많은 나를 도울 수 없었다. 아무도 내가 이걸 어떻게 고칠 수 있는지 아니?
IHandleMessages는 nservicebus의 개념입니다. 코드의 두 번째 평화가 허브와 동일한 * 앱 인스턴스에 위치합니까? 여러 앱 인스턴스에서'ChatHub'을 사용 가능하게하려면 signalr 백플레인을 사용해야합니다. –
@IgorLizunov IHandleMessages는 실제로 NServiceBus 개념입니다. 첫 번째 두 코드 블록은 하나의 앱 인스턴스에 있고 마지막 코드 블록은 다른 앱 인스턴스에 있습니다. 시그널 백플레인으로 이것을 의미합니까? https://www.asp.net/signalr/overview/performance/scaleout-in-signalr – pmulders