我正在尝试使所有已连接的用户都具有身份,但它显示的错误标题为:Uncaught TypeError: signalR.httpConnection is not a constructor
对于此行:
let hubUrl = '/chatHub'
let httpConnection = new signalR.httpConnection(hubUrl);
let hubConnection = new signalR.hubConnection(httpConnection);
hubConnection.on('SetUsersOnline', usersOnline => {
if (usersOnline.length > 0) {
$('#onlineUsers').innerText = '';
$.each(usersOnline, function (i, user) {
addUserOnline(user);
});
}
});
hubConnection.start();
答案 0 :(得分:1)
SignalR的语法经常更改,因此很难说出哪个版本正确和不正确。只需更新到最新的SignalR,然后尝试以下操作即可:
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on('SetUsersOnline', usersOnline => {
if (usersOnline.length > 0) {
$('#onlineUsers').innerText = '';
$.each(usersOnline, function (i, user) {
addUserOnline(user);
});
}
});
connection.start().catch(err => console.error(err.toString()));