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

Restart proxy in case of -1004 error #812

Open
wants to merge 11 commits into
base: minor
Choose a base branch
from
43 changes: 31 additions & 12 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,16 @@ class AudioPlayer {
return duration;
} on PlatformException catch (e) {
try {
// cant connect to servers
if (e.code == "-1004" && source is LockCachingAudioSource) {
// proxy is offline
try {
await _proxy._server.close(force: true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't closing the proxy after this error potentially disrupt other active connections on the proxy? That is unless the -1004 error implies all connections are affected, but it seems possible that the -1004 error could happen on one connection and not another.

} catch (_) {
// ignore err
}
await _proxy.start();
}
throw PlayerException(int.parse(e.code), e.message);
} on FormatException catch (_) {
if (e.code == 'abort') {
Expand Down Expand Up @@ -1995,18 +2005,28 @@ class _ProxyHttpServer {
/// Starts the server.
Future start() async {
_running = true;
_server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
_server.listen((request) async {
if (request.method == 'GET') {
final uriPath = _requestKey(request.uri);
final handler = _handlerMap[uriPath]!;
handler(this, request);
}
}, onDone: () {
_running = false;
}, onError: (Object e, StackTrace st) {
try {
_server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
_server.listen((request) async {
if (request.method == 'GET') {
final uriPath = _requestKey(request.uri);
final handler = _handlerMap[uriPath]!;
handler(this, request);
}
}, onDone: () {
_running = false;
}, onError: (Object e, StackTrace st) async {
_running = false;
try {
await _server.close(force: true);
} catch (_) {
// ignore
}
}, cancelOnError: true);
} catch (_) {
// ignore
_running = false;
});
}
}

/// Stops the server
Expand Down Expand Up @@ -2813,7 +2833,6 @@ class LockCachingAudioSource extends StreamAudioSource {
_downloadProgressSubject.add(percentProgress / 100);
}
}

_progress = 0;
subscription = response.listen((data) async {
_progress += data.length;
Expand Down