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.
212 lines
6.8 KiB
Dart
212 lines
6.8 KiB
Dart
|
3 years ago
|
import 'dart:convert';
|
||
|
|
import 'dart:io';
|
||
|
|
|
||
|
|
import 'package:doctor_app_flutter/screens/live_care/web-rtc/signaling.dart';
|
||
|
|
import 'package:flutter/cupertino.dart';
|
||
|
|
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||
|
|
import 'package:http/io_client.dart';
|
||
|
|
import 'package:signalr_core/signalr_core.dart';
|
||
|
|
|
||
|
|
class SignalRUtil {
|
||
|
|
late String sourceUser;
|
||
|
|
late String destinationUser;
|
||
|
|
setContributors({required String caller, required String receiver}){
|
||
|
|
this.sourceUser = caller;
|
||
|
|
this.destinationUser = receiver;
|
||
|
|
}
|
||
|
|
|
||
|
|
Function(bool)? onConnected;
|
||
|
|
String hubName;
|
||
|
|
SignalRUtil({required this.hubName});
|
||
|
|
|
||
|
|
|
||
|
|
late HubConnection connectionHub;
|
||
|
|
|
||
|
|
closeConnection() async{
|
||
|
|
if(connectionHub != null) {
|
||
|
|
connectionHub.off('OnIncomingCallAsync');
|
||
|
|
connectionHub.off('OnCallDeclinedAsync');
|
||
|
|
connectionHub.off('OnCallAcceptedAsync');
|
||
|
|
connectionHub.off('nHangUpAsync');
|
||
|
|
connectionHub.off('OnIceCandidateAsync');
|
||
|
|
connectionHub.off('OnOfferAsync');
|
||
|
|
await connectionHub.stop();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<bool> openConnection() async {
|
||
|
|
connectionHub = HubConnectionBuilder()
|
||
|
|
.withUrl(
|
||
|
|
hubName,
|
||
|
|
HttpConnectionOptions(
|
||
|
|
logMessageContent: true,
|
||
|
|
client: IOClient(HttpClient()..badCertificateCallback = (x, y, z) => true),
|
||
|
|
logging: (level, message) => print(message),
|
||
|
|
)).build();
|
||
|
|
|
||
|
|
await connectionHub.start();
|
||
|
|
await Future.delayed(Duration(seconds: 1));
|
||
|
|
|
||
|
|
connectionHub.on('ReceiveMessage', (message) {
|
||
|
|
handleIncomingMessage(message);
|
||
|
|
});
|
||
|
|
|
||
|
|
return getConnectionState();
|
||
|
|
}
|
||
|
|
|
||
|
|
void handleIncomingMessage(List<dynamic>? message) {
|
||
|
|
print(message.toString());
|
||
|
|
}
|
||
|
|
|
||
|
|
void sendMessage(List<dynamic> args) async {
|
||
|
|
await connectionHub.invoke('SendMessage', args: args); //['Bob', 'Says hi!']
|
||
|
|
}
|
||
|
|
|
||
|
|
listen({ required Function(CallUser) onAcceptCall, onHangupCall, required Function(Map, CallUser) onDeclineCall, onSdpOffer, onSdpAnswer, required Function(Map) onCandidate}){
|
||
|
|
|
||
|
|
connectionHub.on('OnIncomingCallAsync', (arguments) {
|
||
|
|
print('OnIncomingCallAsync: ${arguments.toString()}');
|
||
|
|
});
|
||
|
|
|
||
|
|
connectionHub.on('OnCallDeclinedAsync', (arguments) {
|
||
|
|
print('OnCallDeclinedAsync: ${arguments.toString()}');
|
||
|
|
final data = json.decode(arguments?.first);
|
||
|
|
onDeclineCall(data, CallUser.from(arguments?.last));
|
||
|
|
});
|
||
|
|
|
||
|
|
connectionHub.on('OnCallAcceptedAsync', (arguments) {
|
||
|
|
print('OnCallAcceptedAsync: ${arguments.toString()}');
|
||
|
|
onAcceptCall(CallUser.from(arguments?.last));
|
||
|
|
});
|
||
|
|
|
||
|
|
connectionHub.on('OnHangUpAsync', (arguments) {
|
||
|
|
print('nHangUpAsync: ${arguments.toString()}');
|
||
|
|
onHangupCall(CallUser.from(arguments?.first));
|
||
|
|
});
|
||
|
|
|
||
|
|
connectionHub.on('OnIceCandidateAsync', (arguments) {
|
||
|
|
print('OnIceCandidateAsync: ${arguments.toString()}');
|
||
|
|
final data = json.decode(arguments?.first);
|
||
|
|
onCandidate(data);
|
||
|
|
});
|
||
|
|
|
||
|
|
connectionHub.on('OnOfferAsync', (arguments) {
|
||
|
|
print('OnOfferAsync: ${arguments.toString()}');
|
||
|
|
final data = json.decode(arguments?.first);
|
||
|
|
onSdpOffer(data, CallUser.from(arguments?.last));
|
||
|
|
});
|
||
|
|
|
||
|
|
connectionHub.on('OnAnswerOffer', (arguments) {
|
||
|
|
print('OnAnswerOffer: ${arguments.toString()}');
|
||
|
|
final sdp = json.decode(arguments?.first)['sdp'];
|
||
|
|
onSdpAnswer(sdp);
|
||
|
|
});
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// CallUserAsyncMobile(string currentUserId, string targerUserId, string patientInfoJson)
|
||
|
|
Future<dynamic> callUserMobile(String from, to) async{
|
||
|
|
final p_info = patientInfo(from, to, PATIENT_PUSH_TOKEN);
|
||
|
|
return await connectionHub.invoke('CallUserAsyncMobile', args: [p_info]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// CallUserAsyncRemote(string currentUserId, string targerUserId)
|
||
|
|
Future<dynamic> callUserRemote(String from, to) async{
|
||
|
|
return await connectionHub.invoke('CallUserAsyncRemote', args: [from, to]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// CallUserAsync(string currentUserId, string targerUserId, string patientInfoJson)
|
||
|
|
Future<dynamic> callUserWeb(String from, to) async{
|
||
|
|
final p_info = patientInfo(from, to, PATIENT_PUSH_TOKEN);
|
||
|
|
return await connectionHub.invoke('CallUserAsync', args: [from, to, p_info]);
|
||
|
|
}
|
||
|
|
patientInfo(String from, to, patientToken){
|
||
|
|
final json_ = {
|
||
|
|
"SessionID" : '123456',
|
||
|
|
"Sess_token" : 'admin',
|
||
|
|
"_Token" : patientToken,
|
||
|
|
"AppointmentNo" : '123456',
|
||
|
|
'ProjectID' : '12',
|
||
|
|
"projectName" : 'Olaya',
|
||
|
|
'doctorname' : 'Dr.Habib',
|
||
|
|
'Docspeciality' : 'Cardiologist',
|
||
|
|
'ClincName' : 'Cardiology',
|
||
|
|
'PatientID' : to,
|
||
|
|
'callerID' : from
|
||
|
|
};
|
||
|
|
return json.encode(json_);
|
||
|
|
}
|
||
|
|
|
||
|
|
// CallDeclinedAsync(string currentUserId, string targerUserId)
|
||
|
|
Future<dynamic> declineCall(String from, to) async{
|
||
|
|
return await connectionHub.invoke('CallDeclinedAsync', args: [from, to]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// AnswerCallAsync(string currentUserId, string targetUserId)
|
||
|
|
Future<dynamic> answerCall(String from, to) async{
|
||
|
|
return await connectionHub.invoke('AnswerCallAsync', args: [from, to]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// IceCandidateAsync(string targetUserId, string candidate)
|
||
|
|
Future<dynamic> addIceCandidate(RTCIceCandidate candidate) async{
|
||
|
|
final target = destinationUser;
|
||
|
|
final _candidate = json.encode({'candidate' : candidate.toMap()});
|
||
|
|
return await connectionHub.invoke('IceCandidateAsync', args: [target, _candidate]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// OfferAsync(string targetUserId,string currentUserId, string targetOffer)
|
||
|
|
Future<dynamic> offer(String from, to, offer) async{
|
||
|
|
return await connectionHub.invoke('OfferAsync', args: [from, to, offer]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// AnswerOfferAsync(string targetUserId, string CallerOffer)
|
||
|
|
Future<dynamic> answerOffer(RTCSessionDescription? answerSdp, caller, receiver) async{
|
||
|
|
final payload = {
|
||
|
|
'target': receiver,
|
||
|
|
'caller': caller,
|
||
|
|
'sdp': answerSdp?.toMap() ?? {},
|
||
|
|
};
|
||
|
|
return await connectionHub.invoke('AnswerOfferAsync', args: [caller, jsonEncode(payload)]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// HangUpAsync(string currentUserId, string targetUserId)
|
||
|
|
Future<dynamic> hangupCall(String from, to) async{
|
||
|
|
return await connectionHub.invoke('HangUpAsync', args: [from, to]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// CallAccepted(string currentUserId,string targetUserId)
|
||
|
|
Future<dynamic> acceptCall(String from, to) async{
|
||
|
|
// return await connectionHub.send(methodName: 'CallAccepted', args: [from, to]);
|
||
|
|
return await connectionHub.invoke("CallAccepted", args: [ from, to]);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
bool getConnectionState() {
|
||
|
|
if (connectionHub.state == HubConnectionState.connected) return true;
|
||
|
|
if (connectionHub.state == HubConnectionState.disconnected) return false;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class CallUser{
|
||
|
|
String? Id;
|
||
|
|
String? UserName;
|
||
|
|
String? Email;
|
||
|
|
String? Phone;
|
||
|
|
String? Title;
|
||
|
|
dynamic? UserStatus;
|
||
|
|
String? Image;
|
||
|
|
int UnreadMessageCount = 0;
|
||
|
|
|
||
|
|
CallUser.from(Map map){
|
||
|
|
Id = map['Id'];
|
||
|
|
UserName = map['UserName'];
|
||
|
|
Email = map['Email'];
|
||
|
|
Phone = map['Phone'];
|
||
|
|
Title = map['Title'];
|
||
|
|
UserStatus = map['UserStatus'];
|
||
|
|
Image = map['Image'];
|
||
|
|
UnreadMessageCount = map['UnreadMessageCount'];
|
||
|
|
}
|
||
|
|
}
|