signalr_core_new 1.0.0 signalr_core_new: ^1.0.0 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 {
print("antes de invoke");
connection.invoke("SendMsgRepository", args: [
"1",
"2",
"1",
"1",
"cristian",
"2",
"prueba",
"room",
"",
"6b933dc4-b679-40a7-853d-0b2ca8900cda",
"Holaaa",
"hjdhkdjhkdjjjhdk",
"",
"",
"",
"",
]);
print("despues de invoke");
} 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();
});
}