Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
SignalR client side method not firing on from server side button click

I am having a Visual Studio 2019 based SignalR Application, where client connects to server.

Following javascript function is called when a page is loaded and it connects to a running server with a successful connectionid.

function Connect() {

        $.connection.hub.url = url;

        stockTickerHubProxy = $.connection.mobileStockTickerHub;
        if (stockTickerHubProxy) {
            $.connection.hub.start().done(function () {
                console.log("Connected...");
                connectionId = $.connection.hub.id;
                console.log(connectionId)
                stockTickerHubProxy.server.setUserName(code);
            })
                .fail(function () {
                    alert("Can't connect");
                })
                ;

            stockTickerHubProxy.client.addMessage = function (name, message) {
                console.log(name + ":" + message);
            }
            stockTickerHubProxy.client.showtradenotification = function (msg) {
                alert(msg)
            }
            
            $.connection.hub.disconnected(function () {
                console.log("Server disconnected.");
            });
            $.connection.hub.reconnecting(function () {
                console.log("Server reconnecting...");
            });
            $.connection.hub.reconnected(function () {
                console.log("Server reconnected...");
                Connect();
            });
            $.connection.hub.error(function (error) {
                console.log('SignalR error: ' + error)
            });
        }

    }

On server, I am executing following test code for checking the function running in javascript html page. Following is the code.

private async void button1_ClickAsync(object sender, EventArgs e)
    {
        mhubContext = GlobalHost.ConnectionManager.GetHubContext<MobileStockTickerHub>();
        await mhubContext.Clients.All.showtradenotification("Hello");
    }

Following is the hub class MobileStockTickerHub

public class MobileStockTickerHub : Hub
{
    
    //Called when a client is connected
    public override Task OnConnected()
    {
        _users.TryAdd(Context.ConnectionId, Context.ConnectionId);
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        string username;
        _users.TryRemove(Context.ConnectionId, out username);
        return base.OnDisconnected(stopCalled);
    }
    public override Task OnReconnected()
    {
        _users.TryAdd(Context.ConnectionId, Context.ConnectionId);
        return base.OnReconnected();
    }
    public string SetUserName(string userName)
    {
        _users[Context.ConnectionId] = userName;
        return "Received ping from " + userName;
    }
    
    
}

Again, when button1_ClickAsync is fired, there is no activity on the webpage, that should fire showtradenotification with an alert message.

Let me know where I am wrong. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Move your client function before your hub.start. You should always register at least one function before your start. See the note regarding in the docs.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda