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.
		
		
		
		
		
			
		
			
				
	
	
		
			90 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			90 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Dart
		
	
import 'dart:developer';
 | 
						|
import 'dart:io';
 | 
						|
import 'package:flutter/material.dart';
 | 
						|
import 'package:http/io_client.dart';
 | 
						|
import 'package:queuing_system/core/config/config.dart';
 | 
						|
import 'package:queuing_system/main.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 = "$BASE_URL/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 {
 | 
						|
    logger.i("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.webSockets,
 | 
						|
              logging: (level, message) => log(message),
 | 
						|
            ))
 | 
						|
        .withAutomaticReconnect()
 | 
						|
        .build();
 | 
						|
 | 
						|
    connection!.serverTimeoutInMilliseconds = 120000;
 | 
						|
    connection!.onclose(onDisconnect);
 | 
						|
    connection!.onreconnecting((exception) => onConnecting());
 | 
						|
    connection!.onreconnected((connectionId) => onConnect());
 | 
						|
    connection!.onreconnected((connectionId) => onConnect());
 | 
						|
 | 
						|
    connection!.on('addChatMessage', (message) => onUpdateAvailable(message));
 | 
						|
 | 
						|
    try {
 | 
						|
      await connection!.start();
 | 
						|
    } catch (e) {
 | 
						|
      logger.i("Exception while connecting:  ${e.toString()}");
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  void sendMessage(List<dynamic> args) async {
 | 
						|
    await connection!.invoke('SendMessage', args: args); //['Bob', 'Says hi!']
 | 
						|
  }
 | 
						|
 | 
						|
  bool getConnectionState() {
 | 
						|
    if (connection == null) return false;
 | 
						|
    logger.i("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();
 | 
						|
//   }
 | 
						|
// }
 | 
						|
}
 |