|
| 1 | +import 'dart:collection'; |
| 2 | +import 'dart:convert'; |
| 3 | + |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:get/get.dart'; |
| 6 | +import 'package:getx_websocket_example/model/user-data-profile.entity.dart'; |
| 7 | +import 'package:web_socket_channel/web_socket_channel.dart'; |
| 8 | + |
| 9 | +import 'logging.service.dart'; |
| 10 | + |
| 11 | +enum Status { loading, error, success } |
| 12 | + |
| 13 | +class DataService2 extends GetxService { |
| 14 | + Worker _getxWorker; |
| 15 | + WebSocketChannel _channel; |
| 16 | + |
| 17 | + final _logger = LoggingService().logger; |
| 18 | + final Rx<UserDataProfile> userDataProfile = UserDataProfile().obs; |
| 19 | + final Rx<Status> userDataProfileStatus = Status.loading.obs; |
| 20 | + |
| 21 | + final ObserverList<Function> _listeners = new ObserverList<Function>(); |
| 22 | + final LinkedHashSet _listenerSet = new LinkedHashSet(); |
| 23 | + |
| 24 | + void fetchUserDataProfile() { |
| 25 | + Function _fetchUserDataProfileCallback = (Map<String, dynamic> _messageEventMap, String _uuid) { |
| 26 | + if (_messageEventMap['event'] == '/identity/$_uuid') { |
| 27 | + userDataProfile(UserDataProfile.fromJson(_messageEventMap['data'])); |
| 28 | + userDataProfileStatus(Status.success); |
| 29 | + _logger.d('---------------userProfile.1: ${userDataProfile.value.uuid}'); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + String uuid = "xyz"; |
| 34 | + String rawJson = '{ "event": "/identity/$uuid", "data": { "uuid": "$uuid", "name": "John Doe", "email": "[email protected]" } }'; |
| 35 | + |
| 36 | + _addListener(uuid, (messageEventMap) => _fetchUserDataProfileCallback(messageEventMap, uuid)); |
| 37 | + _send(rawJson); |
| 38 | + } |
| 39 | + |
| 40 | + _connect() async { |
| 41 | + final _wsUrl = 'wss://echo.websocket.org'; |
| 42 | + if (_channel == null) { |
| 43 | + _logger.d('Connecting ...'); |
| 44 | + _channel = WebSocketChannel.connect(Uri.parse(_wsUrl)); |
| 45 | + _channel.stream.where((event) => event != null).map((event) => jsonDecode(event) as Map<String, dynamic>).where((eventMap) => eventMap['data'] != '404').listen(_onMessage); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + _send(String message) { |
| 50 | + if (_channel != null) { |
| 51 | + if (_channel.sink != null) { |
| 52 | + _channel.sink.add(message); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // --------------------------------------------------------- |
| 58 | + /// Adds a callback to be invoked in case of incoming |
| 59 | + /// notification |
| 60 | + /// --------------------------------------------------------- |
| 61 | + _addListener(String id, Function callback) { |
| 62 | + if (!_listenerSet.contains(id)) _listeners.add(callback); |
| 63 | + _listenerSet.add(id); |
| 64 | + } |
| 65 | + |
| 66 | + _removeListener(Function callback) { |
| 67 | + _listeners.remove(callback); |
| 68 | + } |
| 69 | + |
| 70 | + /// ---------------------------------------------------------- |
| 71 | + /// Callback which is invoked each time that we are receiving |
| 72 | + /// a message from the server |
| 73 | + /// ---------------------------------------------------------- |
| 74 | + _onMessage(message) { |
| 75 | + _listeners.forEach((Function callback) { |
| 76 | + callback(message); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + /// ---------------------------------------------------------- |
| 81 | + /// Closes the WebSocket communication |
| 82 | + /// ---------------------------------------------------------- |
| 83 | + _reset() { |
| 84 | + if (_channel != null) { |
| 85 | + if (_channel.sink != null) { |
| 86 | + _logger.d('Closing connection ...'); |
| 87 | + _channel.sink.close(); |
| 88 | + _connect(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + void onInit() { |
| 95 | + super.onInit(); |
| 96 | + _connect(); |
| 97 | + } |
| 98 | +} |
0 commit comments