Fix websocket connection

This commit is contained in:
Nutcake 2023-09-29 12:30:43 +02:00
parent 73f41ef71e
commit 04ae0687f8
4 changed files with 11 additions and 25 deletions

View file

@ -8,7 +8,7 @@ class Aux {
if (resdb == null || resdb.isEmpty) return ""; if (resdb == null || resdb.isEmpty) return "";
if (resdb.startsWith("http")) return resdb; if (resdb.startsWith("http")) return resdb;
final filename = p.basenameWithoutExtension(resdb); final filename = p.basenameWithoutExtension(resdb);
return "${Config.skyfrostAssetsUrl}$filename"; return "${Config.skyfrostAssetsUrl}/$filename";
} }
} }

View file

@ -5,7 +5,6 @@ import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:hive_flutter/hive_flutter.dart'; import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:contacts_plus_plus/apis/friend_api.dart'; import 'package:contacts_plus_plus/apis/friend_api.dart';
@ -32,7 +31,8 @@ enum EventTarget {
unknown, unknown,
messageSent, messageSent,
receiveMessage, receiveMessage,
messagesRead; messagesRead,
receiveSessionUpdate;
factory EventTarget.parse(String? text) { factory EventTarget.parse(String? text) {
if (text == null) return EventTarget.unknown; if (text == null) return EventTarget.unknown;
@ -286,24 +286,7 @@ class MessagingClient extends ChangeNotifier {
Future<WebSocket> _tryConnect() async { Future<WebSocket> _tryConnect() async {
while (true) { while (true) {
try { try {
final http.Response response; final ws = await WebSocket.connect(Config.resoniteHubUrl.replaceFirst("https://", "wss://"), headers: _apiClient.authorizationHeader);
try {
response = await http.post(
Uri.parse("${Config.resoniteHubUrl}/negotiate"),
headers: _apiClient.authorizationHeader,
);
_apiClient.checkResponse(response);
} catch (e) {
throw "Failed to acquire connection info from Neos API: $e";
}
final body = jsonDecode(response.body);
final url = (body["url"] as String?)?.replaceFirst("https://", "wss://");
final wsToken = body["accessToken"];
if (url == null || wsToken == null) {
throw "Invalid response from server.";
}
final ws = await WebSocket.connect("$url&access_token=$wsToken");
_attempts = 0; _attempts = 0;
return ws; return ws;
} catch (e) { } catch (e) {
@ -383,6 +366,10 @@ class MessagingClient extends ChangeNotifier {
} }
notifyListeners(); notifyListeners();
break; break;
case EventTarget.receiveSessionUpdate:
// TODO: Handle session updates
_logger.info("Received session update");
break;
} }
} }

View file

@ -1,6 +1,5 @@
class Config { class Config {
static const String apiBaseUrl = "https://api.resonite.com"; static const String apiBaseUrl = "https://api.resonite.com";
static const String durianAssetsUrl = "https://assets.everion.com";
static const String skyfrostAssetsUrl = "https://assets.resonite.com"; static const String skyfrostAssetsUrl = "https://assets.resonite.com";
static const String resoniteHubUrl = "$apiBaseUrl/hub"; static const String resoniteHubUrl = "$apiBaseUrl/hub";
static const String secretClientKey = ""; static const String secretClientKey = "";

View file

@ -22,15 +22,15 @@ class Friend implements Comparable {
bool get isHeadless => userStatus.activeSessions.any((session) => session.headlessHost == true && session.hostUserId == id); bool get isHeadless => userStatus.activeSessions.any((session) => session.headlessHost == true && session.hostUserId == id);
factory Friend.fromMap(Map map) { factory Friend.fromMap(Map map) {
final userStatus = UserStatus.fromMap(map["userStatus"]); final userStatus = map["userStatus"] == null ? UserStatus.empty() : UserStatus.fromMap(map["userStatus"]);
return Friend( return Friend(
id: map["id"], id: map["id"],
username: map["friendUsername"], username: map["contactUsername"],
ownerId: map["ownerId"] ?? map["id"], ownerId: map["ownerId"] ?? map["id"],
// Neos bot status is always offline but should be displayed as online // Neos bot status is always offline but should be displayed as online
userStatus: map["id"] == _neosBotId ? userStatus.copyWith(onlineStatus: OnlineStatus.online) : userStatus, userStatus: map["id"] == _neosBotId ? userStatus.copyWith(onlineStatus: OnlineStatus.online) : userStatus,
userProfile: UserProfile.fromMap(map["profile"] ?? {}), userProfile: UserProfile.fromMap(map["profile"] ?? {}),
friendStatus: FriendStatus.fromString(map["friendStatus"]), friendStatus: FriendStatus.fromString(map["contactStatus"]),
latestMessageTime: map["latestMessageTime"] == null latestMessageTime: map["latestMessageTime"] == null
? DateTime.fromMillisecondsSinceEpoch(0) : DateTime.parse(map["latestMessageTime"]), ? DateTime.fromMillisecondsSinceEpoch(0) : DateTime.parse(map["latestMessageTime"]),
); );