import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:socket_io_client/socket_io_client.dart'; import '../app/app.logger.dart'; class MySocketIoClient { final log = getLogger('MySocketIoClient'); final String _url = dotenv.env['url']!; static final MySocketIoClient _instance = MySocketIoClient._internal(); factory MySocketIoClient() => _instance; MySocketIoClient._internal(); late Socket _socket; Socket get socket => _socket; Future init() async { try { _socket = io(_url, { 'transports': ['websocket'], 'autoConnect': false, }); _socket.connect(); log.i('socket connected'); } catch (e) { log.e('error : $e'); } } Future emit(String event, dynamic data) async { _socket.emit(event, data); } Future on(String event, Function(dynamic) callback) async { _socket.on(event, callback); } Future off(String event) async { _socket.off(event); } Future disconnect() async { _socket.disconnect(); } Future connect() async { _socket.connect(); } }