2024-08-10 14:36:08 -04:00
|
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
2024-08-10 16:38:46 -04:00
|
|
|
import '../pb/stillbox.pb.dart';
|
2024-08-10 14:36:08 -04:00
|
|
|
|
2024-08-12 09:13:43 -04:00
|
|
|
class Client {
|
2024-08-10 16:38:46 -04:00
|
|
|
late Uri _wsUri;
|
2024-08-10 14:36:08 -04:00
|
|
|
late WebSocketChannel channel;
|
|
|
|
|
2024-08-12 09:13:43 -04:00
|
|
|
Client() {
|
2024-08-10 14:36:08 -04:00
|
|
|
String socketUrl = 'ws://xenon:3050/ws';
|
|
|
|
Uri baseUri = Uri.base;
|
|
|
|
if (baseUri.scheme == 'http' || baseUri.scheme == 'https') {
|
2024-08-12 09:13:43 -04:00
|
|
|
final port = (baseUri.hasPort ? ':${baseUri.port}' : '');
|
|
|
|
socketUrl =
|
|
|
|
'${baseUri.scheme == 'http' ? 'ws' : 'wss'}://${baseUri.host}$port/ws';
|
2024-08-10 14:36:08 -04:00
|
|
|
}
|
2024-08-10 16:38:46 -04:00
|
|
|
_wsUri = Uri.parse(socketUrl);
|
|
|
|
}
|
2024-08-10 14:36:08 -04:00
|
|
|
|
2024-08-12 09:13:43 -04:00
|
|
|
bool isConnected() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-10 16:38:46 -04:00
|
|
|
void connect() {
|
|
|
|
channel = WebSocketChannel.connect(_wsUri);
|
2024-08-10 14:36:08 -04:00
|
|
|
channel.stream.listen((event) => _handleData(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleData(dynamic event) {
|
2024-08-10 16:38:46 -04:00
|
|
|
final msg = Message.fromBuffer(event);
|
|
|
|
switch (msg.whichToClientMessage()) {
|
|
|
|
case Message_ToClientMessage.call:
|
|
|
|
case Message_ToClientMessage.notification:
|
|
|
|
case Message_ToClientMessage.popup:
|
|
|
|
case Message_ToClientMessage.error:
|
2024-08-10 17:07:04 -04:00
|
|
|
default:
|
2024-08-10 16:38:46 -04:00
|
|
|
}
|
2024-08-10 14:36:08 -04:00
|
|
|
}
|
|
|
|
}
|