Created
January 30, 2024 17:32
-
-
Save brasizza/a7741bad4f5140543003d6b050e3154b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:developer'; | |
import 'dart:io'; | |
import 'package:cinema_stub/app/core/socket/socket_base.dart'; | |
class SocketCore implements SocketBase { | |
late final Socket socket; | |
Map<String, Function> channels = {}; | |
@override | |
Future<void> init({required String ip, required String port}) async { | |
if (ip != '' && port != '') { | |
try { | |
socket = await Socket.connect(ip, int.parse(port), timeout: const Duration(seconds: 5)); | |
socket.listen( | |
(data) { | |
Map<String, dynamic> serverResponse = jsonDecode(String.fromCharCodes(data)); | |
log(serverResponse.toString()); | |
if (serverResponse.containsKey('channel')) { | |
final String key = serverResponse['channel']; | |
if (channels.containsKey(key)) { | |
channels[key]!(serverResponse); | |
} | |
} | |
}, | |
); | |
} catch (e, s) { | |
log('Socket server is not reachable: $e', error: e, stackTrace: s); | |
} | |
} | |
} | |
@override | |
void addChannel(String method, Function(dynamic p1) f) { | |
String key = method; | |
if (!channels.containsKey(key)) { | |
channels[key] = f; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment