Reputation: 2119
I created a server with SignalR and SQLTableDependency. After that, I created a project with Vue and SignalR Javascript Client and everything works, the notification subscription in the server execute a SignalR method to send the object to all the Clients
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);
}
}
In JavaScript Client I made this:
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());
});
The result of that is this:
And finally my C# Client made with .Net Core 2.1. This is not working
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);
}
}
In void main
Console app I call the Ejecutar
method:
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();
NOTE: In the server, the CORS are activated to allow anything.
Upvotes: 0
Views: 811
Reputation: 392
Are you using Direct mode ? The direct mode does not function with this. Turn the Direct mode off.
Upvotes: 2
Reputation: 2119
Ok, in connection.on
I use a List, but instead of that, I used a Class with the properties like the server send. So now, it's working:
connection.On<Result>("RegistrarTarea", (result) => {
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(result.Cambio);
});
Upvotes: 0