mirror of
https://github.com/amigan/calls.git
synced 2025-01-31 05:22:37 -05:00
ws test
This commit is contained in:
parent
b45899558c
commit
596c9863ef
7 changed files with 125 additions and 52 deletions
|
@ -1,24 +1,22 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:web_socket_channel/io.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../pb/stillbox.pb.dart';
|
||||
import 'play.dart';
|
||||
import 'storage_none.dart'
|
||||
if (dart.library.io) 'storage_secure.dart'
|
||||
if (dart.library.html) 'storage_web.dart';
|
||||
import 'stillbox_none.dart'
|
||||
if (dart.library.io) 'stillbox_io.dart'
|
||||
if (dart.library.html) 'stillbox_web.dart';
|
||||
|
||||
class BadAuthException implements Exception {}
|
||||
|
||||
class Stillbox extends ChangeNotifier {
|
||||
final storage = Storer();
|
||||
Player player = Player();
|
||||
late IOWebSocketChannel channel;
|
||||
final channel = Socketer();
|
||||
bool connected = false;
|
||||
late Uri _wsUri;
|
||||
LiveState _state = LiveState.LS_LIVE;
|
||||
Filter? currentFilter;
|
||||
Call? _currentCall;
|
||||
Map<String, String> headers = {};
|
||||
Uri? baseUri = Uri.base;
|
||||
|
||||
set state(LiveState newState) {
|
||||
|
@ -54,15 +52,6 @@ class Stillbox extends ChangeNotifier {
|
|||
}
|
||||
}
|
||||
|
||||
void updateCookie(http.Response response) {
|
||||
String? rawCookie = response.headers['set-cookie'];
|
||||
if (rawCookie != null) {
|
||||
int index = rawCookie.indexOf(';');
|
||||
headers['cookie'] =
|
||||
(index == -1) ? rawCookie : rawCookie.substring(0, index);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> doLogin(String uri, String username, String password) async {
|
||||
baseUri = Uri.parse(uri);
|
||||
setUris();
|
||||
|
@ -80,10 +69,10 @@ class Stillbox extends ChangeNotifier {
|
|||
body: form,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
updateCookie(response);
|
||||
String? token = channel.updateCookie(response);
|
||||
storage.setKey('baseURL', uri);
|
||||
if (!kIsWeb) {
|
||||
storage.setKey('token', headers['cookie']!);
|
||||
if (!kIsWeb && token != null) {
|
||||
storage.setKey('token', token);
|
||||
}
|
||||
await connect();
|
||||
return true;
|
||||
|
@ -95,7 +84,7 @@ class Stillbox extends ChangeNotifier {
|
|||
if (connected == true) {
|
||||
return;
|
||||
}
|
||||
channel = IOWebSocketChannel.connect(_wsUri, headers: headers);
|
||||
channel.connect(_wsUri);
|
||||
channel.stream.listen((event) => _handleData(event), onDone: () {
|
||||
connected = false;
|
||||
}, onError: (error) {
|
||||
|
@ -112,7 +101,7 @@ class Stillbox extends ChangeNotifier {
|
|||
if (storedToken == null || storedUri == null) {
|
||||
throw (BadAuthException);
|
||||
}
|
||||
headers['cookie'] = storedToken;
|
||||
channel.setCookie(storedToken);
|
||||
baseUri = Uri.parse(storedUri);
|
||||
setUris();
|
||||
}
|
||||
|
|
47
lib/controller/stillbox_io.dart
Normal file
47
lib/controller/stillbox_io.dart
Normal file
|
@ -0,0 +1,47 @@
|
|||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:web_socket_channel/io.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class Storer {
|
||||
final storage = const FlutterSecureStorage();
|
||||
Future<String?> getKey(String key) async {
|
||||
return await storage.read(key: key);
|
||||
}
|
||||
|
||||
void setKey(String key, String value) async {
|
||||
await storage.write(key: key, value: value);
|
||||
}
|
||||
}
|
||||
|
||||
class Socketer {
|
||||
late IOWebSocketChannel channel;
|
||||
Map<String, String> headers = {};
|
||||
|
||||
WebSocketSink get sink {
|
||||
return channel.sink;
|
||||
}
|
||||
|
||||
Stream<dynamic> get stream {
|
||||
return channel.stream;
|
||||
}
|
||||
|
||||
String? updateCookie(http.Response response) {
|
||||
String? rawCookie = response.headers['set-cookie'];
|
||||
if (rawCookie != null) {
|
||||
int index = rawCookie.indexOf(';');
|
||||
headers['cookie'] =
|
||||
(index == -1) ? rawCookie : rawCookie.substring(0, index);
|
||||
}
|
||||
|
||||
return headers['cookie'];
|
||||
}
|
||||
|
||||
void setCookie(String token) {
|
||||
headers['cookie'] = token;
|
||||
}
|
||||
|
||||
void connect(Uri uri) {
|
||||
channel = IOWebSocketChannel.connect(uri, headers: headers);
|
||||
}
|
||||
}
|
34
lib/controller/stillbox_none.dart
Normal file
34
lib/controller/stillbox_none.dart
Normal file
|
@ -0,0 +1,34 @@
|
|||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class Storer {
|
||||
Future<String?> getKey(String key) async {
|
||||
return null;
|
||||
}
|
||||
|
||||
void setKey(String key, String value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
class Socketer {
|
||||
late WebSocketChannel channel;
|
||||
|
||||
WebSocketSink get sink {
|
||||
return channel.sink;
|
||||
}
|
||||
|
||||
Stream<dynamic> get stream {
|
||||
return channel.stream;
|
||||
}
|
||||
|
||||
String? updateCookie(http.Response response) {
|
||||
return null;
|
||||
}
|
||||
|
||||
void setCookie(String token) {}
|
||||
|
||||
void connect(Uri uri) {
|
||||
channel = WebSocketChannel.connect(uri);
|
||||
}
|
||||
}
|
35
lib/controller/stillbox_web.dart
Normal file
35
lib/controller/stillbox_web.dart
Normal file
|
@ -0,0 +1,35 @@
|
|||
import 'dart:html';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class Storer {
|
||||
Future<String?> getKey(String key) async {
|
||||
return window.localStorage[key];
|
||||
}
|
||||
|
||||
void setKey(String key, String value) {
|
||||
window.localStorage[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
class Socketer {
|
||||
late WebSocketChannel channel;
|
||||
|
||||
WebSocketSink get sink {
|
||||
return channel.sink;
|
||||
}
|
||||
|
||||
Stream<dynamic> get stream {
|
||||
return channel.stream;
|
||||
}
|
||||
|
||||
String? updateCookie(http.Response response) {
|
||||
return response.headers['set-cookie'];
|
||||
}
|
||||
|
||||
void setCookie(String token) {}
|
||||
|
||||
void connect(Uri uri) {
|
||||
channel = WebSocketChannel.connect(uri);
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
class Storer {
|
||||
Future<String?> getKey(String key) async {
|
||||
return null;
|
||||
}
|
||||
|
||||
void setKey(String key, String value) {
|
||||
return;
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
class Storer {
|
||||
final storage = const FlutterSecureStorage();
|
||||
Future<String?> getKey(String key) async {
|
||||
return await storage.read(key: 'token');
|
||||
}
|
||||
|
||||
void setKey(String key, String value) async {
|
||||
await storage.write(key: 'token', value: value);
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
import 'dart:html';
|
||||
|
||||
class Storer {
|
||||
Future<String?> getKey(String key) async {
|
||||
return window.localStorage[key];
|
||||
}
|
||||
|
||||
void setKey(String key, String value) {
|
||||
window.localStorage[key] = value;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue