Skip to content

Commit

Permalink
Reconnection Logic Impl
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacakakpo1 committed Sep 23, 2024
1 parent 830dd73 commit ee94e60
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
switch (message.socketMethod) {
case SocketMethod.BYE:
{
//make sure to disconnect the telnyxclient on Bye for Decline
// make sure to disconnect the telnyxclient on Bye for Decline
// Only disconnect the socket when the call was ended from push notifications
print("telnyxClient disconnected");
telnyxClient.disconnect();
Expand Down
4 changes: 2 additions & 2 deletions packages/telnyx_webrtc/lib/model/push_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class PushNotification {
}

class PushMetaData {
PushMetaData({this.caller_name, this.caller_number, this.call_id});
PushMetaData(
{this.caller_name, this.caller_number, this.call_id, this.voice_sdk_id});

String? caller_name;
String? caller_number;
Expand All @@ -18,7 +19,6 @@ class PushMetaData {
bool? isAnswer;
bool? isDecline;


PushMetaData.fromJson(Map<dynamic, dynamic> json) {
caller_name = json['caller_name'];
caller_number = json['caller_number'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ReceivedMessage {
StateParams? stateParams;
IncomingInviteParams? inviteParams;
DialogParams? dialogParams;
String? voiceSdkId;

ReceivedMessage(
{this.jsonrpc,
Expand All @@ -18,8 +19,8 @@ class ReceivedMessage {
this.reattachedParams,
this.stateParams,
this.inviteParams,
this.dialogParams
});
this.dialogParams,
this.voiceSdkId});

ReceivedMessage.fromJson(Map<String, dynamic> json) {
jsonrpc = json['jsonrpc'];
Expand All @@ -33,9 +34,14 @@ class ReceivedMessage {
inviteParams = json['params'] != null
? IncomingInviteParams.fromJson(json['params'])
: null;
if(json['params']['dialogParams'] != null){
if (json['params']['dialogParams'] != null) {
dialogParams = DialogParams.fromJson(json['params']['dialogParams']);
}

if (json['params']['voice_sdk_id'] != null) {
voiceSdkId = json['params']['voice_sdk_id'];
Logger().i('Voice SDK ID: $voiceSdkId');
}
}

Map<String, dynamic> toJson() {
Expand Down Expand Up @@ -71,16 +77,13 @@ class ReceivedResult {
String? sessId;
TelnyxSocketError? error;

ReceivedResult(
{this.jsonrpc,
this.id,
this.resultParams});
ReceivedResult({this.jsonrpc, this.id, this.resultParams});

ReceivedResult.fromJson(Map<String, dynamic> json) {
jsonrpc = json['jsonrpc'];
id = json['id'];
resultParams =
json['result'] != null ? ResultParams.fromJson(json['result']) : null;
json['result'] != null ? ResultParams.fromJson(json['result']) : null;
sessId = json['sessid'];
error = json['error'] != null
? TelnyxSocketError.fromJson(json['error'])
Expand All @@ -104,7 +107,6 @@ class ReceivedResult {
}
}


class ReattachedParams {
List<dynamic>? reattachedSessions;

Expand Down Expand Up @@ -136,12 +138,11 @@ class ReattachedParams {
class ResultParams {
StateParams? stateParams;

ResultParams(
{this.stateParams});
ResultParams({this.stateParams});

ResultParams.fromJson(Map<String, dynamic> json) {
stateParams =
json['params'] != null ? StateParams.fromJson(json['params']) : null;
json['params'] != null ? StateParams.fromJson(json['params']) : null;
}

Map<String, dynamic> toJson() {
Expand All @@ -152,6 +153,7 @@ class ResultParams {
return data;
}
}

class StateParams {
String? state;

Expand Down
40 changes: 34 additions & 6 deletions packages/telnyx_webrtc/lib/telnyx_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import 'dart:async';
import 'dart:convert';
import 'dart:ffi';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:telnyx_webrtc/model/verto/send/attach_call_message.dart';
Expand All @@ -25,6 +24,7 @@ import 'model/call_state.dart';
import 'model/jsonrpc.dart';
import 'model/push_notification.dart';
import 'model/verto/send/pong_message_body.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';

typedef OnSocketMessageReceived = void Function(TelnyxMessage message);
typedef OnSocketErrorReceived = void Function(TelnyxSocketError message);
Expand Down Expand Up @@ -87,6 +87,26 @@ class TelnyxClient {
String ringtonePath = "";
String ringBackpath = "";
PushMetaData? _pushMetaData;
StreamSubscription<InternetConnectionStatus>? _connectionCheckerSubscription;

final connectionChecker = InternetConnectionChecker();

void checkReconnection() {
final _connectionCheckerSubscription =
connectionChecker.onStatusChange.listen(
(InternetConnectionStatus status) {
if (status == InternetConnectionStatus.connected) {
if (activeCalls().isNotEmpty) {
_reconnectToSocket();
}
} else {
//
}
},
);

// Remember to cancel the subscription when it's no longer needed
}

TelnyxClient() {
// Default implementation of onSocketMessageReceived
Expand Down Expand Up @@ -209,6 +229,7 @@ class TelnyxClient {
void _connectWithCallBack(
PushMetaData? pushMetaData, OnOpenCallback openCallback) {
_logger.i('connect() ${pushMetaData?.toJson()}');
_pushMetaData = pushMetaData;
try {
if (pushMetaData?.voice_sdk_id != null) {
txSocket.hostAddress =
Expand All @@ -220,6 +241,7 @@ class TelnyxClient {
txSocket.hostAddress = _storedHostAddress;
_logger.i('connecting to WebSocket $_storedHostAddress');
}

txSocket.connect();

txSocket.onOpen = () {
Expand All @@ -228,6 +250,7 @@ class TelnyxClient {
_logger.i('Web Socket is now connected');
_onOpen();
openCallback.call();
_connectionCheckerSubscription?.cancel();
};

txSocket.onMessage = (dynamic data) {
Expand Down Expand Up @@ -579,11 +602,7 @@ class TelnyxClient {
}

void updateCall(Call call) {
if (calls.containsKey(call.callId)) {
calls[call.callId!] = call;
} else {
calls[call.callId!] = call;
}
calls[call.callId!] = call;
}

/// Closes the socket connection, effectively logging the user out.
Expand Down Expand Up @@ -611,6 +630,8 @@ class TelnyxClient {
}

/// Closes the socket connection, effectively logging the user out.
/// You must recconnect to the socket connection to continue using the SDK or may result in
/// Unhandled Exception: Bad state: StreamSink is closed
void disconnect() {
_invalidateGatewayResponseTimer();
_resetGatewayCounters();
Expand Down Expand Up @@ -831,6 +852,13 @@ class TelnyxClient {
Call offerCall = _createCall();
offerCall.callId = invite.inviteParams?.callID;
updateCall(offerCall);

//set voice_sdk_id
_pushMetaData = PushMetaData(
caller_number: null,
caller_name: null,
voice_sdk_id: message.message.voiceSdkId);

onSocketMessageReceived.call(message);

offerCall.callHandler
Expand Down
1 change: 1 addition & 0 deletions packages/telnyx_webrtc/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
audioplayers: ^5.2.1
assets_audio_player: ^3.1.1
shared_preferences: ^2.2.3
internet_connection_checker: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
flutter_callkit_incoming: ^2.0.4+1
shared_preferences: ^2.2.3
permission_handler: ^11.3.1
internet_connection_checker: ^2.0.0


dev_dependencies:
Expand Down

0 comments on commit ee94e60

Please sign in to comment.