信号集线器如何使用IHubContext c#
我需要一些帮助,知道他们是否对信号员做了错误的事情。放手: 我的应用程序是mvc。我遵循了教程的示例并这样做了: 我创建了一个实时类:信号集线器如何使用IHubContext c#,c#,asp.net-core,signalr,C#,Asp.net Core,Signalr,我需要一些帮助,知道他们是否对信号员做了错误的事情。放手: 我的应用程序是mvc。我遵循了教程的示例并这样做了: 我创建了一个实时类: public class RealTime : Hub { public async Task SendMessage(string message) { await Clients.All.SendAsync("ReceiveMessage", message); } } 启动时: public void Confi
public class RealTime : Hub
{
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("ReceiveMessage", message);
}
}
启动时:
public void ConfigureServices(IServiceCollection services)
{
.
.
.
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
.
.
.
app.UseSignalR(routes =>
{
routes.MapHub<RealTime>("/realtime");
});
}
在我的前端,我做了以下工作:
var connection = new signalR.HubConnectionBuilder().withUrl("/realtime").build();
connection.on("ReceiveMessage", function (message) {
Swal.fire(
'The Internet?',
message,
'question'
);
});
connection.start().then(function () {
//
}).catch(function (err) {
//
});
但什么也没发生。后端和前端都没有错误。但是什么也没发生
有人能告诉我我做错了什么吗?我注意到的第一件事是,您将集线器注入了一个控制器,然后尝试将它注入到该控制器中调用的服务/管理器中。 第二件事是,当您不在实现Hub的类中时,最好使用
SendCoreAsync()
而不是sendsync
。
我建议您做的第三件事是放置.configureLogging(signar.LogLevel.Information),然后放置.build()代码>
另外,将此设置为不获取错误:import*作为来自“@aspnet/signal”的信号器代码>
同时启动连接并显示消息,以了解连接是否已建立
connection.start().then(function () {
console.log("signalR connected")
}).catch(function (err) {
console.log(err)
});
只有在启动连接后,我才会打开连接
connection.on("ReceiveMessage", (message) =>{
Swal.fire(
'The Internet?',
message,
'question'
);
});
希望我能帮助你 谢谢你的帮助。我找到了解决办法,尽管它很管用
在打开连接之前,我输入了以下代码段:
Object.defineProperty(WebSocket, 'OPEN', { value: 1, });
所以我的js是这样的:
var connection = new signalR.HubConnectionBuilder()
.withUrl("/realtime")
.configureLogging(signalR.LogLevel.Debug)
.build();
Object.defineProperty(WebSocket, 'OPEN', { value: 1, });
connection.start().then(function () {
console.log("signalR connected")
}).catch(function (err) {
return console.error(err)
});
connection.on("ReceiveMessage", function (message) {
Swal.fire(
'The Internet?',
message,
'question'
);
});
您的服务器端代码看起来是正确的。在浏览器(F12)中打开开发工具并检查JavaScript控制台。有错误吗?此外,如果在运行connection.start()
之前打开开发工具,则可以在“网络”选项卡中查看流量。查看连接是否打开,以及消息是否真的返回。My console.log没有错误。在“网络”选项卡中有一篇帖子,标题为:请求URL:请求方法:帖子远程地址:127.0.0.1:44375状态代码:200版本:HTTP/2.0参考者策略:降级时无参考者您应该看到对/realtime
的第二个请求,状态为101交换协议
,该请求保持打开状态。你看到了吗?如果有,你应该有一个“消息”选项卡,在那里你可以看到来回发送的消息。没有。只有第一个。可能是前端问题?如果我将start更改为:connection.start()。然后(函数(){/})。catch(函数(err){return console.error(err.toString());});在Cosole.log中,我看到:WebSocket未处于打开状态
Object.defineProperty(WebSocket, 'OPEN', { value: 1, });
var connection = new signalR.HubConnectionBuilder()
.withUrl("/realtime")
.configureLogging(signalR.LogLevel.Debug)
.build();
Object.defineProperty(WebSocket, 'OPEN', { value: 1, });
connection.start().then(function () {
console.log("signalR connected")
}).catch(function (err) {
return console.error(err)
});
connection.on("ReceiveMessage", function (message) {
Swal.fire(
'The Internet?',
message,
'question'
);
});