我猜我编写的程序有点错误,所以当我从客户端调用集线器的func时,它会无休止地进入,并且我从服务器得到的响应无休止,大约间隔半秒。这就是客户的代码,我想问题不在于此。
<input type="text" id="inputMessage"/>
<button class="submit" id="sendButton"></button>
///////////////////////////////////////////////// /////////////////////////
let hubUrl = "/Chat";
const hubConnection = new signalR.HubConnectionBuilder()
.withUrl(hubUrl)
.build();
hubConnection.on("Receive", function (message, userName) {
//some html stuff, just putting message and userName into divs
});
document.getElementById("sendButton").addEventListener("click", function (e) {
let userName = "@User.Identity.Name";
let message = document.getElementById("inputMessage").value;
hubConnection.invoke("Send", message, userName);
});
hubConnection.start();
那是我的ChatHub:
public class ChatHub : Hub {
public async Task Send(string message, string username)
{
await this.Clients.All.SendAsync("Receive", message, username);
}
}
我调试了我的代码,它表明当它进入Send(string message,string username)时,它会无休止地执行它。 That's what I get. 如您所见,左侧有很多相同的响应,但应该只是一个。
答案 0 :(得分:0)
问题出在我添加的某些脚本中。
<script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script>
<script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script>
<script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script>
那些引起了我的问题。