diff --git a/CHANGELOG.md b/CHANGELOG.md index 799a405..4a9d2ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,4 +51,8 @@ ## 0.1.1 -* Null Safety Support \ No newline at end of file +* Null Safety Support + +## 0.1.2 + +* IsConnected Method Added \ No newline at end of file diff --git a/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalR.kt b/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalR.kt index 437083f..3f4c404 100644 --- a/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalR.kt +++ b/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalR.kt @@ -3,10 +3,7 @@ package dev.asdevs.signalr_flutter import android.os.Handler import android.os.Looper import io.flutter.plugin.common.MethodChannel.Result -import microsoft.aspnet.signalr.client.Credentials -import microsoft.aspnet.signalr.client.LogLevel -import microsoft.aspnet.signalr.client.Logger -import microsoft.aspnet.signalr.client.SignalRFuture +import microsoft.aspnet.signalr.client.* import microsoft.aspnet.signalr.client.hubs.HubConnection import microsoft.aspnet.signalr.client.hubs.HubProxy import microsoft.aspnet.signalr.client.transport.LongPollingTransport @@ -16,6 +13,7 @@ enum class CallMethod(val value: String) { ConnectToServer("connectToServer"), Reconnect("reconnect"), Stop("stop"), + IsConnected("isConnected"), ListenToHubMethod("listenToHubMethod"), InvokeServerMethod("invokeServerMethod") } @@ -115,6 +113,21 @@ object SignalR { } } + fun isConnected(result: Result) { + try { + if (this::connection.isInitialized) { + when (connection.state) { + ConnectionState.Connected -> result.success(true) + else -> result.success(false) + } + } else { + result.success(false) + } + } catch (ex: Exception) { + result.error("Error", ex.localizedMessage, null) + } + } + fun listenToHubMethod(methodName: String, result: Result) { try { hub.on(methodName, { res -> diff --git a/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalRFlutterPlugin.kt b/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalRFlutterPlugin.kt index 7f514e1..e0c9a2d 100644 --- a/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalRFlutterPlugin.kt +++ b/android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalRFlutterPlugin.kt @@ -55,6 +55,9 @@ public class SignalRFlutterPlugin : FlutterPlugin, MethodCallHandler { CallMethod.Stop.value -> { SignalR.stop(result) } + CallMethod.IsConnected.value -> { + SignalR.isConnected(result) + } CallMethod.ListenToHubMethod.value -> { if (call.arguments is String) { val methodName = call.arguments as String diff --git a/example/android/build.gradle b/example/android/build.gradle index 578862b..7d84ac3 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -1,12 +1,12 @@ buildscript { - ext.kotlin_version = '1.4.21' + ext.kotlin_version = '1.4.32' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.2' + classpath 'com.android.tools.build:gradle:4.1.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 501d387..c99bbf0 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -14,9 +14,9 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/signalr_flutter/ios" SPEC CHECKSUMS: - Flutter: 0e3d915762c693b495b44d77113d4970485de6ec + Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c signalr_flutter: f36ec358cbff2fdaacb27af943c011bfe40a0d91 PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c -COCOAPODS: 1.9.3 +COCOAPODS: 1.10.1 diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 1d526a1..919434a 100644 --- a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/example/lib/main.dart b/example/lib/main.dart index 566e8e3..22850c3 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,7 +14,8 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { String _signalRStatus = 'Unknown'; late SignalR signalR; - final GlobalKey rootScaffoldMessengerKey = GlobalKey(); + final GlobalKey rootScaffoldMessengerKey = + GlobalKey(); @override void initState() { @@ -24,8 +25,10 @@ class _MyAppState extends State { // Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { - signalR = SignalR('', "", - hubMethods: [""], + signalR = SignalR( + '', + "", + hubMethods: [""], statusChangeCallback: _onStatusChange, hubCallback: _onNewMessage); } @@ -55,7 +58,12 @@ class _MyAppState extends State { floatingActionButton: FloatingActionButton( child: Icon(Icons.cast_connected), onPressed: () async { - await signalR.connect(); + final isConnected = await signalR.isConnected ?? false; + if (!isConnected) { + await signalR.connect(); + } else { + signalR.stop(); + } }, ), ), diff --git a/example/pubspec.lock b/example/pubspec.lock index 063bb90..03530a5 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -94,7 +94,7 @@ packages: path: ".." relative: true source: path - version: "0.1.1" + version: "0.1.2" sky_engine: dependency: transitive description: flutter diff --git a/ios/Classes/SignalR.swift b/ios/Classes/SignalR.swift index 0b0caed..38bc786 100644 --- a/ios/Classes/SignalR.swift +++ b/ios/Classes/SignalR.swift @@ -8,7 +8,7 @@ import Foundation enum CallMethod : String { - case connectToServer, reconnect, stop, invokeServerMethod, listenToHubMethod + case connectToServer, reconnect, stop, isConnected, invokeServerMethod, listenToHubMethod } class SignalRWrapper { @@ -104,6 +104,19 @@ class SignalRWrapper { } } + func isConnected(result: @escaping FlutterResult) { + if let connection = self.connection { + switch connection.state { + case .connected: + result(true) + default: + result(false) + } + } else { + result(false) + } + } + func listenToHubMethod(methodName : String, result: @escaping FlutterResult) { if let hub = self.hub { hub.on(methodName) { (args) in diff --git a/ios/Classes/SwiftSignalrFlutterPlugin.swift b/ios/Classes/SwiftSignalrFlutterPlugin.swift index eade1d3..f6291d7 100644 --- a/ios/Classes/SwiftSignalrFlutterPlugin.swift +++ b/ios/Classes/SwiftSignalrFlutterPlugin.swift @@ -33,6 +33,10 @@ public class SwiftSignalRFlutterPlugin: NSObject, FlutterPlugin { SignalRWrapper.instance.stop(result: result) break + case CallMethod.isConnected.rawValue: + SignalRWrapper.instance.isConnected(result: result) + break + case CallMethod.listenToHubMethod.rawValue: let methodName = call.arguments as! String SignalRWrapper.instance.listenToHubMethod(methodName: methodName, result: result) diff --git a/lib/signalr_flutter.dart b/lib/signalr_flutter.dart index 5d01fb2..8a11c25 100644 --- a/lib/signalr_flutter.dart +++ b/lib/signalr_flutter.dart @@ -91,6 +91,18 @@ class SignalR { } } + Future get isConnected async { + try { + return await _channel.invokeMethod("isConnected"); + } on PlatformException catch (ex) { + print("Platform Error: ${ex.message}"); + return Future.error(ex.message!); + } on Exception catch (ex) { + print("Error: ${ex.toString()}"); + return Future.error(ex.toString()); + } + } + @Deprecated( "This method no longer works on iOS. For now it may work on Android but this will be removed later. Consider using constructor parameter [hubMethods]") diff --git a/pubspec.yaml b/pubspec.yaml index 5be1cff..99a4593 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: signalr_flutter description: A flutter plugin for .net SignalR client. This client is for ASP.Net SignalR, not for .Net Core SignalR. -version: 0.1.1 +version: 0.1.2 homepage: https://github.com/AS-Devs/signalr_flutter environment: diff --git a/test/signalr_flutter_test.dart b/test/signalr_flutter_test.dart index a38a13e..cf9a739 100644 --- a/test/signalr_flutter_test.dart +++ b/test/signalr_flutter_test.dart @@ -13,7 +13,6 @@ void main() async { switch (methodCall.method) { case "connectToServer": return true; - break; case "invokeServerMethod": return { 'baseUrl': "123", @@ -21,9 +20,7 @@ void main() async { }; default: return PlatformException( - code: "Error", - message: - "No implementation found for method ${methodCall.method}"); + code: "Error", message: "No implementation found for method ${methodCall.method}"); } }); }); @@ -47,4 +44,3 @@ void main() async { }); }); } -