Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda