You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
queuing_system/lib/utils/signalR_utils.dart

78 lines
2.9 KiB
Dart

import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/io_client.dart';
import 'package:signalr_core/signalr_core.dart';
/*
https://vcallapi.hmg.com/patientcalling.html?IPAddress=10.10.14.20&Clinic=4&Project=15
---------------------------------------
IP, Clinic, Project Mapping to SignalR
---------------------------------------
(SignalR: 10.10.14.11) <<<< [IP=10.10.14.20 | Clinic=4 | Project=15]
(SignalR: 10.70.249.21) <<<< [IP=10.10.14.20 | Clinic=3 | Project=16]
---------------------------------------
*/
class SignalRHelper {
// String hubBaseURL = "https://vcallapi.hmg.com/PatientCallingHub";
// String hubBaseURL = "https://vcallapi.hmg.com/PatientCallingHub";
String hubBaseURL = "https://ms.hmg.com/nscapi/PatientCallingHub";
// String hubBaseURL = "https://vcallapi.hmg.com/PatientCallingHub?IPAddress=10.10.14.20";
// "https://VCallApi.hmg.com/WebRTCHub?source=mobile&username=2001273";
HubConnection? connection;
startSignalRConnection(String deviceIp,
{required Function(dynamic) onUpdateAvailable, required VoidCallback onConnect, required Function(dynamic) onDisconnect, required VoidCallback onConnecting}) async {
log("Connecting Signal R with: $deviceIp");
final url = hubBaseURL + "?IPAddress=$deviceIp";
// final url = hubBaseURL;
connection = HubConnectionBuilder()
.withUrl(
url,
HttpConnectionOptions(
client: IOClient(HttpClient()..badCertificateCallback = (x, y, z) => true),
transport: HttpTransportType.serverSentEvents,
logging: (level, message) => log(message),
))
.build();
connection!.onclose(onDisconnect);
connection!.onreconnecting((exception) => onConnecting());
connection!.onreconnected((connectionId) => onConnect());
connection!.on('addChatMessage', (message) => onUpdateAvailable(message));
await connection!.start();
}
void sendMessage(List<dynamic> args) async {
await connection!.invoke('SendMessage', args: args); //['Bob', 'Says hi!']
}
bool getConnectionState() {
if (connection == null) return false;
log("connectionState: ${connection!.state}");
if (connection!.state == HubConnectionState.connected || connection!.state == HubConnectionState.connecting) return true;
if (connection!.state == HubConnectionState.disconnected || connection!.state == HubConnectionState.disconnecting) return false;
return false;
}
closeConnection() async {
if (connection!.state == HubConnectionState.connected || connection!.state == HubConnectionState.connecting) {
await connection!.stop();
}
}
startConnection(BuildContext context) async {
if (connection!.state == HubConnectionState.connected || connection!.state == HubConnectionState.connecting) {
connection!.off('addChatMessage');
await connection!.start();
}
}
}