Skip to content

Commit

Permalink
Merge pull request #29 from AS-Devs/development
Browse files Browse the repository at this point in the history
Version - 0.1.2
  • Loading branch information
AyonAB authored May 3, 2021
2 parents a70004d + c8c8d3e commit c14ba5c
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 22 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@

## 0.1.1

* Null Safety Support
* Null Safety Support

## 0.1.2

* IsConnected Method Added
21 changes: 17 additions & 4 deletions android/src/main/kotlin/dev/asdevs/signalr_flutter/SignalR.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +13,7 @@ enum class CallMethod(val value: String) {
ConnectToServer("connectToServer"),
Reconnect("reconnect"),
Stop("stop"),
IsConnected("isConnected"),
ListenToHubMethod("listenToHubMethod"),
InvokeServerMethod("invokeServerMethod")
}
Expand Down Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
String _signalRStatus = 'Unknown';
late SignalR signalR;
final GlobalKey<ScaffoldMessengerState> rootScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
final GlobalKey<ScaffoldMessengerState> rootScaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();

@override
void initState() {
Expand All @@ -24,8 +25,10 @@ class _MyAppState extends State<MyApp> {

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
signalR = SignalR('<Your SignalR Url>', "<Your Hub Name>",
hubMethods: ["<Hub Method Name>"],
signalR = SignalR(
'<Your server url here>',
"<Your hub name here>",
hubMethods: ["<Your Hub Method Names>"],
statusChangeCallback: _onStatusChange,
hubCallback: _onNewMessage);
}
Expand Down Expand Up @@ -55,7 +58,12 @@ class _MyAppState extends State<MyApp> {
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();
}
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.1.1"
version: "0.1.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down
15 changes: 14 additions & 1 deletion ios/Classes/SignalR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

enum CallMethod : String {
case connectToServer, reconnect, stop, invokeServerMethod, listenToHubMethod
case connectToServer, reconnect, stop, isConnected, invokeServerMethod, listenToHubMethod
}

class SignalRWrapper {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions ios/Classes/SwiftSignalrFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions lib/signalr_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ class SignalR {
}
}

Future<bool?> get isConnected async {
try {
return await _channel.invokeMethod<bool>("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]")

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
6 changes: 1 addition & 5 deletions test/signalr_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ void main() async {
switch (methodCall.method) {
case "connectToServer":
return true;
break;
case "invokeServerMethod":
return <String, dynamic>{
'baseUrl': "123",
"hubName": "456",
};
default:
return PlatformException(
code: "Error",
message:
"No implementation found for method ${methodCall.method}");
code: "Error", message: "No implementation found for method ${methodCall.method}");
}
});
});
Expand All @@ -47,4 +44,3 @@ void main() async {
});
});
}

0 comments on commit c14ba5c

Please sign in to comment.