WebSocket failed to connect and tried many ways but still got the problem

Discussão em 'Angular' iniciado por Long, Outubro 27, 2024.

  1. Long

    Long Guest

    My error:

    "WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled"

    I got this problem although i still can connect to server without warning in console log on the website but the error always appears in my terminal when i ran the programme.

    My Angular code:

    public connect(): void {
    this.hubConnection = new signalR.HubConnectionBuilder()
    .withUrl(this.webSockeUrl, {
    transport: signalR.HttpTransportType.WebSockets,
    skipNegotiation:true,
    accessTokenFactory: () => { return this.token!},
    })
    .withAutomaticReconnect()
    .build();

    this.hubConnection
    .start()
    .then(() => console.log("Connection started"))
    .catch(err => console.error("Error establishing connection: ", err));
    }

    public addReceiveMessageListener(): void {
    this.hubConnection.on("ReceiveMessage", (message: string) => {
    console.log("Receive Message from server:", message);
    });

    this.hubConnection.on("ReceivePrivateMessage", (message: string) => {
    console.log("Receive Message from another client:", message);
    });
    }


    And i still received message from another client through connectionId with this code and authorized normally in Asp.net:

    [Authorize]
    public class ChatHub : Hub
    {
    public async Task SendMessage(string connectionId, string message)
    {
    await Clients.Client(connectionId).SendAsync("ReceivePrivateMessage", message);
    }

    public override async Task OnConnectedAsync()
    {
    var connectionId = Context.ConnectionId;
    await Clients.All.SendAsync("ReceiveMessage", $"{connectionId} has joined");
    }
    }


    if i remove skipNegotiation i got negotiation with server incomplete, i also tried add my server port is 44398 to inbound rules of firewall and tried turn window feature "WebSocket protocol" in control panel on. I read many articles involve with this problem and tried but i still can not fix it does not appear in my terminal.

    Thanks for help!

    Continue reading...

Compartilhe esta Página