signalr_core_new 1.0.1 signalr_core_new: ^1.0.1 copied to clipboard
ASP.NET Core SignalR Client written for Dart that is platform-independent, and can be used on both the command-line and the browser.
example/main.dart
import 'dart:async';
import 'package:signalr_core_new/signalr_core_new.dart';
Future<void> main(List<String> arguments) async {
HubConnection? connection = HubConnectionBuilder()
.withUrl(
"https://kd45cn5z-13690.brs.devtunnels.ms/chathub",
HttpConnectionOptions(
accessTokenFactory: () async =>
"518c317b-74e1-45b9-aef3-5bcefbfe4de1",
))
.withAutomaticReconnect([0]).build();
try {
await connection.start();
print("Conexion iniciada.");
} catch (e) {
print("Error al iniciar la conexión: $e");
}
connection.onclose((error) {
if (error != null) {
print("CERRAR CONEXIÓN: Conexión SignalR cerrada con error: $error");
} else {
print("CERRAR CONEXIÓN: Conexión SignalR cerrada sin error");
}
//Estado: HubConnectionState.disconnected
});
connection.onreconnecting((error) {
print("ESTADO DE CONEXIÓN: Reconectando...${connection.state}");
if (connection.state == HubConnectionState.reconnecting) {
print("Entrando en condicional 1");
}
});
connection.onreconnected((connectionId) {
try {
connection.invoke("KeepAlivePing", args: ["identifiacod"]);
} catch (ex) {
print("error $ex");
}
print("ESTADO DE CONEXIÓN: Reconectado");
});
try {
connection.invoke("SendMsgRepository", args: []);
} catch (ex) {
print("error $ex");
}
Timer.periodic(Duration(seconds: 10), (timer) {
// Si deseas detener el intervalo después de cierto tiempo
//print(connection.state);
//connection.stop();
});
}