'Asp.Net SignalR sometimes not connected and doesn't show any error codes
I have a project that uses SignalR to make three web pages communicate with each other. Open with chrome browser on different computers, only one of them can work normally. After further inspection, on a computer with a working chrome browser, an exception will also occur when the Edge browser is opened.
Further explanation, I have three webpages A, B and C, and I have joined the same group. Under normal circumstances, if any webpage triggers the function, the other two will receive the message. Webpage B does not receive any messages at all, and there is no error message about abnormal connection. However, the function can still be triggered normally on page B, and pages A and C receive messages normally.
I use the following code to confirm that SignalR is working
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected()
{
return base.OnDisconnected();
}
When my SignalR is connected normally, OnConnected() is triggered, and when the webpage is closed, OnDisconnected() is triggered.
Use Debug in any browser on any computer, A and C webpages are triggered normally.
The B page is almost never triggered in the chrome browser of some computers, or the Edge browser of any computer (there is a small chance that it will trigger and work normally).
In the state where OnConnected() is not triggered, the B webpage can still send the SignalR function, but cannot receive messages.
Code changed from Group to All
Clients.Group(group_name).startPartIdSend(part_code);
↓
Clients.All.startPartIdSend(part_code);
Still can't receive messages, so I think it's not the reason for the group. I really don't have any idea, can someone help me?
The code for using SignalR on page B is as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Human_anatomy.aspx.cs" Inherits="ThreeDirectionImages_Human_anatomy" %>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Human_anatomy_Display</title>
<script src="../Scripts/jquery-3.4.1.min.js"></script>
<script src="../Scripts/jquery.signalR-2.4.1.min.js"></script>
<script src="../signalr/hubs"></script>
<script>
$(document).ready(function () {
var chat = $.connection.chatHub;
$.connection.hub.start().done(function () {
var userID = window.opener.document.getElementById("roomName").value;//Get UserID
console.log("userID" + ":" + userID)
chat.server.updateWeb("ThreeDirectionImages", userID);//join group
});
});
</script>
Startup.cs:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(IPCTest.Startup))]
namespace IPCTest
{
public partial class Startup {
public void Configuration(IAppBuilder app) {
ConfigureAuth(app);
app.MapSignalR();
}
}
}
ChatHub.cs usage example:
public void CoordinateTransferServerSendToClient(float x, float y, int z, int part_code, int user_id)
{
Connector source = connectionlist.Find(a => a.connectionId.Contains(Context.ConnectionId));
string group_name = source.groupName;
Clients.Group(group_name).serverSendToClient(x, y, z, part_code, user_id);
//Clients.All.serverSendToClient(x, y, z, part_code, user_id);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|