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.
mohemm-flutter-app/lib/utils/signalR_utils.dart

86 lines
3.1 KiB
Dart

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";
String msg = "Awaiting Patients Arrival";
HubConnection connection;
startSignalRConnection(String deviceIp,
{@required Function(dynamic) onUpdateAvailable, @required VoidCallback onConnect, @required Function(dynamic) onDisconnect, @required VoidCallback onConnecting}) async {
// Hardcoded IP For Testing
// deviceIp = "10.10.14.11";
print("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) => {
print(message),
},
))
.build();
connection.onclose(onDisconnect);
connection.onreconnecting((exception) => onConnecting());
connection.onreconnected((connectionId) => onConnect());
connection.on('addChatMessage', (message) {
onUpdateAvailable(message);
});
// try {
await connection.start();
// } catch (e, s) {
// print("Here the error: ${e.toString()}");
// }
}
void sendMessage(List<dynamic> args) async {
await connection.invoke('SendMessage', args: args); //['Bob', 'Says hi!']
}
bool getConnectionState() {
if (connection == null) return false;
if (connection.state == HubConnectionState.connected || connection.state == HubConnectionState.connecting) return true;
if (connection.state == HubConnectionState.disconnected || connection.state == HubConnectionState.disconnecting) return false;
}
closeConnection(BuildContext context) 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();
}
}
}