Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ramin/make_proxy_aware_connection_configurable #310

Merged
15 changes: 12 additions & 3 deletions lib/services/connection/api_manager/binary_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ class BinaryAPI extends BaseAPI {
BinaryAPI({
String? key,
bool enableDebug = false,
this.proxyAwareConnection = false,
}) : super(key: key ?? '${UniqueKey()}', enableDebug: enableDebug);

static const Duration _disconnectTimeOut = Duration(seconds: 5);
static const Duration _websocketConnectTimeOut = Duration(seconds: 10);

/// A flag to indicate if the connection is proxy aware.
final bool proxyAwareConnection;

/// Represents the active websocket connection.
///
/// This is used to send and receive data from the websocket server.
Expand Down Expand Up @@ -77,10 +81,15 @@ class BinaryAPI extends BaseAPI {
_logDebugInfo('connecting to $uri.');

await _setUserAgent();
final String proxy = await FlutterSystemProxy.findProxyFromEnvironment(
uri.toString().replaceAll('wss', 'https'));

final HttpClient client = HttpClient()..findProxy = (Uri uri) => proxy;
HttpClient? client;

if (proxyAwareConnection) {
final String proxy = await FlutterSystemProxy.findProxyFromEnvironment(
uri.toString().replaceAll('wss', 'https'));

client = HttpClient()..findProxy = (Uri uri) => proxy;
}

// Initialize connection to websocket server.
_webSocketChannel = IOWebSocketChannel.connect('$uri',
Expand Down
6 changes: 6 additions & 0 deletions lib/state/connection/connection_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ class ConnectionCubit extends Cubit<ConnectionState> {
ConnectionInformation connectionInformation, {
BaseAPI? api,
this.enableDebug = false,
// TODO(NA): Refactor to only get BinaryAPI instance. and printResponse and proxyAwareConnection can be part of BinaryAPI only.
this.printResponse = false,
this.proxyAwareConnection = false,
}) : super(const ConnectionInitialState()) {
APIInitializer().initialize(
api: api ??
BinaryAPI(
key: _key,
proxyAwareConnection: proxyAwareConnection,
enableDebug: enableDebug,
),
);
Expand Down Expand Up @@ -55,6 +58,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
/// Default value is `false`.
final bool printResponse;

/// A flag to indicate if the connection is proxy aware.
final bool proxyAwareConnection;

// In some devices like Samsung J6 or Huawei Y7, the call manager doesn't response to the ping call less than 5 sec.
final Duration _pingTimeout = const Duration(seconds: 5);

Expand Down
Loading