我是signalR核心的初学者,我正在尝试学习它。我逐步遵循了video,除了 Client方法在siganlR中调用了4次之外,其他所有工作都正常吗?
结果:
枢纽:
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace signalR
{
public class Chat:Hub
{
public async Task SendMessage(string user,string message)
{
await Clients.All.SendAsync("ForwardToClients", user,message);
}
}
}
Javascript:
var connection = new signalR.HubConnectionBuilder().withUrl("/chat").configureLogging(signalR.LogLevel.Information).build();
connection.on("ForwardToClients", (user, message) => {
const encoding = user + "Says:" + message;
const li = document.createElement("li");
li.textContent = encoding;
document.getElementById("messagesList").appendChild(li);
});
connection.start().catch(er => console.log(er.toString()));
document.getElementById("sendMessage").addEventListener("click", event => {
const user = document.getElementById("userInput").value;
const message = document.getElementById("messageInput").value;
connection.invoke("SendMessage", user, message).catch(er => console.log(er.toString()));
});
请问有人可以帮我吗? !