SignalR C#客户端没有调用方法

问题描述 投票:0回答:2

我用SignalR和SQLTableDependency创建了一个服务器。之后,我用Vue和SignalR Javascript Client创建了一个项目,一切正常,服务器中的通知订阅执行SignalR方法将对象发送给所有客户端

private void Changed(object sender, RecordChangedEventArgs<Todo> eventArgs)
    {
        if(eventArgs.ChangeType != TableDependency.SqlClient.Base.Enums.ChangeType.None)
        {
            var changedEntity = eventArgs.Entity;
            var mensaje = TipoCambios(eventArgs);
            _hubContext.Clients.All.SendAsync("RegistrarTarea", changedEntity);
        }
    }

在JavaScript客户端我做了这个:

coneccionTodo.on("RegistrarTarea", todos => {
    this.$refs.alerta.Abrir(todos.cambio, "info", "Alerta");
    console.log(todos);
  });
  coneccionTodo
    .start()
    .then(response => {
      this.sinConexion = false;
    })
    .catch(error => {
      console.log("Error Todo SignalR", error.toString());
    });

结果是这样的:

enter image description here

最后我的C#Client使用.Net Core 2.1制作。这不起作用

public static async Task Ejecutar() {
    connection.On<List<dynamic>>("RegistrarTarea", (objects) => {
        Console.WriteLine(objects);
    });
    try
    {                                
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine($"Conexión exitosa a {url}");
        await connection.StartAsync();
        //await connection.InvokeAsync("RegistrarTarea", "Consola", true);
    }
    catch (Exception ex)
    {
        SignalR_Exception(ex);
    }
}

void main控制台应用程序中,我调用Ejecutar方法:

    connection = new HubConnectionBuilder().WithUrl(url).Build();
    connection.Closed += async (error) => {
        await Task.Delay(new Random().Next(0, 5) * 1000);
        await connection.StartAsync();
    };
    Task.Run(() => Ejecutar());
    Console.ReadLine();

注意:在服务器中,CORS被激活以允许任何操作。

javascript c# asp.net-core signalr asp.net-core-2.1
2个回答
2
投票

你在使用Direct模式吗?直接模式不起作用。关闭直接模式。


0
投票

好吧,在connection.on中我使用了List,但是我使用了一个类似于服务器发送的类。现在,它的工作原理是:

    connection.On<Result>("RegistrarTarea", (result) => {                
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(result.Cambio);
    });

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.