fix: 兼容 Web 同源部署地址
This commit is contained in:
@@ -3,11 +3,16 @@ import 'package:flutter/foundation.dart';
|
|||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class SettingsProvider with ChangeNotifier {
|
class SettingsProvider with ChangeNotifier {
|
||||||
// Use 10.0.2.2 for Android emulator to access host's localhost
|
// On web: use empty string so API calls use same origin (works behind any proxy)
|
||||||
static String get _defaultUrl =>
|
// On Android emulator: 10.0.2.2 maps to host localhost
|
||||||
(defaultTargetPlatform == TargetPlatform.android && !kIsWeb)
|
// On other platforms: localhost
|
||||||
? "http://10.0.2.2:8080"
|
static String get _defaultUrl {
|
||||||
: "http://localhost:8080";
|
if (kIsWeb) return "";
|
||||||
|
if (defaultTargetPlatform == TargetPlatform.android) {
|
||||||
|
return "http://10.0.2.2:8080";
|
||||||
|
}
|
||||||
|
return "http://localhost:8080";
|
||||||
|
}
|
||||||
|
|
||||||
String _baseUrl = _defaultUrl;
|
String _baseUrl = _defaultUrl;
|
||||||
Color _themeColor = Colors.blue;
|
Color _themeColor = Colors.blue;
|
||||||
@@ -106,8 +111,18 @@ class SettingsProvider with ChangeNotifier {
|
|||||||
|
|
||||||
// Also provide the RTMP URL based on the same hostname
|
// Also provide the RTMP URL based on the same hostname
|
||||||
String get rtmpUrl {
|
String get rtmpUrl {
|
||||||
final uri = Uri.parse(_baseUrl);
|
final host = _baseUrl.isEmpty ? _effectiveHost : Uri.parse(_baseUrl).host;
|
||||||
return "rtmp://${uri.host}:1935/live";
|
return "rtmp://$host:1935/live";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback hostname when baseUrl is empty (web same-origin mode)
|
||||||
|
String get _effectiveHost {
|
||||||
|
if (kIsWeb) {
|
||||||
|
final host = Uri.base.host;
|
||||||
|
if (host.isNotEmpty) return host;
|
||||||
|
return 'localhost';
|
||||||
|
}
|
||||||
|
return 'localhost';
|
||||||
}
|
}
|
||||||
|
|
||||||
String playbackUrl(String roomId, {String? quality}) {
|
String playbackUrl(String roomId, {String? quality}) {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class ChatService {
|
|||||||
Stream<ChatMessage> get messages => _messageController.stream;
|
Stream<ChatMessage> get messages => _messageController.stream;
|
||||||
|
|
||||||
void connect(String baseUrl, String roomId, String username) {
|
void connect(String baseUrl, String roomId, String username) {
|
||||||
final wsUri = Uri.parse(baseUrl).replace(
|
final wsUri = _webSocketUri(baseUrl).replace(
|
||||||
scheme: 'ws',
|
scheme: 'ws',
|
||||||
path: '/api/ws/room/$roomId',
|
path: '/api/ws/room/$roomId',
|
||||||
queryParameters: {'username': username},
|
queryParameters: {'username': username},
|
||||||
@@ -83,6 +83,21 @@ class ChatService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uri _webSocketUri(String baseUrl) {
|
||||||
|
if (baseUrl.isEmpty) {
|
||||||
|
if (kIsWeb) {
|
||||||
|
return Uri.base.replace(
|
||||||
|
scheme: Uri.base.scheme == 'https' ? 'wss' : 'ws',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Uri.parse('http://localhost:8080');
|
||||||
|
}
|
||||||
|
|
||||||
|
final uri = Uri.parse(baseUrl);
|
||||||
|
final scheme = uri.scheme == 'https' ? 'wss' : 'ws';
|
||||||
|
return uri.replace(scheme: scheme);
|
||||||
|
}
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_channel?.sink.close();
|
_channel?.sink.close();
|
||||||
_messageController.close();
|
_messageController.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user