backend: separate App and server, add websocket endpoint, accept data directory command argument. initialize sqlalchemy database and models in new module. frontend: create backend class for managing ws connection, move ui to chat_screen, create scaffold chat and agent classes.
This commit is contained in:
1 parent
1e82796db1
commit
44f0b3589c
19 files changed
+936
-122
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import 'dart:html';
|
||||
import 'dart:convert';
|
||||
|
||||
class Backend {
|
||||
static WebSocket? _socket;
|
||||
static String? _url;
|
||||
|
||||
static void setup(String url) {
|
||||
_url = url;
|
||||
connect();
|
||||
}
|
||||
|
||||
static void auth(username) {
|
||||
_socket!.send(
|
||||
json.encode({'username': username})
|
||||
);
|
||||
}
|
||||
|
||||
static void connect() {
|
||||
_socket = WebSocket(_url!);
|
||||
_socket!.onOpen.listen((event) {
|
||||
print('Connected to the WebSocket server');
|
||||
auth("the-user");
|
||||
ping();
|
||||
});
|
||||
_socket!.onError.listen((event) {
|
||||
print('Error: $event');
|
||||
_reconnect();
|
||||
});
|
||||
_socket!.onClose.listen((event) {
|
||||
print('Disconnected. Attempting to reconnect...');
|
||||
_reconnect();
|
||||
});
|
||||
}
|
||||
|
||||
static void _reconnect() {
|
||||
Future.delayed(Duration(seconds: 5), () {
|
||||
connect();
|
||||
});
|
||||
}
|
||||
|
||||
static void ping() {
|
||||
if (_socket != null && _socket!.readyState == WebSocket.OPEN) {
|
||||
final data = json.encode({'path': 'ping'});
|
||||
_socket!.send(data);
|
||||
print('Ping sent: $data');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user