[英]Multiple signalR connections on the same hub from a single user
我在我的 ASP.NET Core MVC 项目中使用了 SignalR。 用户页面中的连接字符串为:
var connection = new signalR.HubConnectionBuilder().withUrl("/signalRServer").withAutomaticReconnect().build();
connection.start().then( result => {
console.log("Connection has been established suucessfully.")
我使用此连接字符串来获取即时通知,并且效果很好。 最近,我尝试向用户页面添加另一个功能。 当用户上传文件时,我想向管理页面发送信号。 当调用以下 function 时,此信号将发送到集线器:
<script>
function sendNotification(pmId, pmNumber){
var notificationText = "PM" + pmNumber + "uploaded";
var formData = new FormData();
formData.append("notificationText", notificationText);
$.ajax({
type: "POST",
url: "/Page/SendUploadNotification",
processData: false,
contentType: false,
data: formData,
success: function(response){
if(response.success){
var notificationId = response.notificationId;
connection.invoke("FileUploadNotification", notificationId, notificationText).catch(function(err){
return console.error(err)
});
location.reload();
}else{
alert("Error!");
}
}
});
}
</script>
问题是在调用上面的function时,不能使用前面已经定义好的signalR连接。 控制台说:
Uncaught ReferenceError: connection is not defined at Object.success (pmPage:655:21) at c (jquery-3.6.0.min.js:2:28327) at Object.fireWith [as resolveWith] (jquery-3.6.0. min.js:2:29072) 在 l (jquery-3.6.0.min.js:2:79901) 在 XMLHttpRequest。 (jquery-3.6.0.min.js:2:82355)
我该如何解决这个问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.