Skip to content

Commit

Permalink
web_socket_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mirmoktadir committed Aug 22, 2023
1 parent 0420eda commit bd0f58e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 6 deletions.
16 changes: 16 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,11 @@ PODS:
- gRPC-Core/Interface (= 1.50.1)
- gRPC-Core/Interface (1.50.1)
- GTMSessionFetcher/Core (3.1.1)
- image_cropper (0.0.4):
- Flutter
- TOCropViewController (~> 2.6.1)
- image_picker_ios (0.0.1):
- Flutter
- leveldb-library (1.22.2)
- nanopb (2.30909.0):
- nanopb/decode (= 2.30909.0)
Expand All @@ -852,6 +857,7 @@ PODS:
- Flutter
- FMDB (>= 2.7.5)
- SwiftyGif (5.4.4)
- TOCropViewController (2.6.1)

DEPENDENCIES:
- cloud_firestore (from `.symlinks/plugins/cloud_firestore/ios`)
Expand All @@ -864,6 +870,8 @@ DEPENDENCIES:
- Flutter (from `Flutter`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- flutter_native_splash (from `.symlinks/plugins/flutter_native_splash/ios`)
- image_cropper (from `.symlinks/plugins/image_cropper/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `.symlinks/plugins/sqflite/ios`)
Expand Down Expand Up @@ -894,6 +902,7 @@ SPEC REPOS:
- ReachabilitySwift
- SDWebImage
- SwiftyGif
- TOCropViewController

EXTERNAL SOURCES:
cloud_firestore:
Expand All @@ -916,6 +925,10 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_local_notifications/ios"
flutter_native_splash:
:path: ".symlinks/plugins/flutter_native_splash/ios"
image_cropper:
:path: ".symlinks/plugins/image_cropper/ios"
image_picker_ios:
:path: ".symlinks/plugins/image_picker_ios/ios"
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
shared_preferences_foundation:
Expand Down Expand Up @@ -952,6 +965,8 @@ SPEC CHECKSUMS:
"gRPC-C++": 0968bace703459fd3e5dcb0b2bed4c573dbff046
gRPC-Core: 17108291d84332196d3c8466b48f016fc17d816d
GTMSessionFetcher: e8647203b65cee28c5f73d0f473d096653945e72
image_cropper: a3291c624a953049bc6a02e1f8c8ceb162a24b25
image_picker_ios: 4a8aadfbb6dc30ad5141a2ce3832af9214a705b5
leveldb-library: f03246171cce0484482ec291f88b6d563699ee06
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
Expand All @@ -961,6 +976,7 @@ SPEC CHECKSUMS:
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
sqflite: 31f7eba61e3074736dff8807a9b41581e4f7f15a
SwiftyGif: 93a1cc87bf3a51916001cf8f3d63835fb64c819f
TOCropViewController: edfd4f25713d56905ad1e0b9f5be3fbe0f59c863

PODFILE CHECKSUM: fff3348f19d060f45045a9c9fbca3a264147c2f7

Expand Down
2 changes: 1 addition & 1 deletion ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C8080294A63A400263BE5 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PostDetailView extends GetView<HomeController> {
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);

return Scaffold(
appBar: AppBar(
title: const Text('Post Detail'),
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Future<void> main() async {
minTextAdapt: true,
splitScreenMode: true,
useInheritedMediaQuery: true,
rebuildFactor: RebuildFactors.all,
rebuildFactor: (old, data) => true,
builder: (context, widget) {
return GetMaterialApp(
title: "GetXStandard",
Expand Down
53 changes: 53 additions & 0 deletions lib/utils/web_socket_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'dart:convert';
import 'dart:io';
import 'package:get/get.dart';
import 'package:logger/logger.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class WebSocketManager {
late WebSocketChannel _channel;
late WebSocket _webSocket;
RxString receivedMessage = "".obs; // This full response comes from websocket
RxInt totalClaps =
0.obs; // collecting any particular value from socket response

Future<void> connectWebSocket() async {
const String url = 'your-websocket-url';
try {
_webSocket = await WebSocket.connect(url);
_channel = IOWebSocketChannel(_webSocket);

_channel.stream.listen((message) {
_parseReceivedMessage(message); // your particular value
receivedMessage.value = message; // main response
});
} catch (e) {
Logger().e('WebSocket connection failed: $e');
}
}

// Decoding main socket response to get particular value
void _parseReceivedMessage(String message) {
final Map<String, dynamic> data = jsonDecode(message);
if (data.containsKey('total_claps')) {
totalClaps.value = data['total_claps'];
}
}

void sendMessage(String message) {
if (_webSocket.readyState == WebSocket.open) {
_webSocket.add(message);
} else {
Logger().e('WebSocket is not open.');
}
}

void closeWebSocket() {
_channel.sink.close();
Logger().i("WebSocket connection closed!");
}
}

// TODO: how to use ?
// call WebsocketManager.connectWebSocket() into any getx controller onReady methode to initiate websocket
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,10 @@ packages:
dependency: "direct main"
description:
name: flutter_screenutil
sha256: "1b61f8c4cbf965104b6ca7160880ff1af6755aad7fec70b58444245132453745"
sha256: "8cf100b8e4973dc570b6415a2090b0bfaa8756ad333db46939efc3e774ee100d"
url: "https://pub.dev"
source: hosted
version: "5.8.4"
version: "5.9.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -1374,7 +1374,7 @@ packages:
source: hosted
version: "0.1.4-beta"
web_socket_channel:
dependency: transitive
dependency: "direct main"
description:
name: web_socket_channel
sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies:
connectivity_plus: ^4.0.2
image_picker: ^1.0.2
image_cropper: ^5.0.0
web_socket_channel: ^2.4.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit bd0f58e

Please sign in to comment.