From 0a0cbcf09b81af9dcb8926583644aa6bd43efb2e Mon Sep 17 00:00:00 2001 From: progman <91064515+proggen-com@users.noreply.github.com> Date: Thu, 27 Oct 2022 12:37:18 +0200 Subject: [PATCH] Allow reinit (#80) * allow reinit of pusher * allow reinit of pusher-js * Bump to version 2.1.0 Co-authored-by: Niek Co-authored-by: Pusher CI --- CHANGELOG.md | 6 + .../PusherChannelsFlutterPlugin.kt | 45 ++++--- .../SwiftPusherChannelsFlutterPlugin.swift | 115 +++++++++--------- lib/pusher_channels_flutter_web.dart | 4 + pubspec.lock | 4 +- pubspec.yaml | 2 +- 6 files changed, 93 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34fd7bf..3256bae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2.1.0 + +* [CHANGED] Allow reinitialization of the pusher singleton +* [CHANGED] Add subscription count event handling ios/android +* [CHANGED] Update flutter dependencies to the latest versions. + ## 2.0.2 * [FIXED] Fix private-encrypted channels subscriptions diff --git a/android/src/main/kotlin/com/pusher/channels_flutter/PusherChannelsFlutterPlugin.kt b/android/src/main/kotlin/com/pusher/channels_flutter/PusherChannelsFlutterPlugin.kt index ebcad4b..37e06dd 100644 --- a/android/src/main/kotlin/com/pusher/channels_flutter/PusherChannelsFlutterPlugin.kt +++ b/android/src/main/kotlin/com/pusher/channels_flutter/PusherChannelsFlutterPlugin.kt @@ -97,30 +97,29 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw result: Result ) { try { - if (pusher == null) { - val options = PusherOptions() - if (call.argument("cluster") != null) options.setCluster(call.argument("cluster")) - if (call.argument("useTLS") != null) options.isUseTLS = - call.argument("useTLS")!! - if (call.argument("activityTimeout") != null) options.activityTimeout = - call.argument("activityTimeout")!! - if (call.argument("pongTimeout") != null) options.pongTimeout = - call.argument("pongTimeout")!! - if (call.argument("maxReconnectionAttempts") != null) options.maxReconnectionAttempts = - call.argument("maxReconnectionAttempts")!! - if (call.argument("maxReconnectGapInSeconds") != null) options.maxReconnectGapInSeconds = - call.argument("maxReconnectGapInSeconds")!! - if (call.argument("authEndpoint") != null) options.channelAuthorizer = - HttpChannelAuthorizer(call.argument("authEndpoint")) - if (call.argument("authorizer") != null) options.channelAuthorizer = this - if (call.argument("proxy") != null) { - val (host, port) = call.argument("proxy")!!.split(':') - options.proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port.toInt())) - } - pusher = Pusher(call.argument("apiKey"), options) - } else { - throw Exception("Pusher Channels already initialized.") + if (pusher != null) { + pusher!!.disconnect() + } + val options = PusherOptions() + if (call.argument("cluster") != null) options.setCluster(call.argument("cluster")) + if (call.argument("useTLS") != null) options.isUseTLS = + call.argument("useTLS")!! + if (call.argument("activityTimeout") != null) options.activityTimeout = + call.argument("activityTimeout")!! + if (call.argument("pongTimeout") != null) options.pongTimeout = + call.argument("pongTimeout")!! + if (call.argument("maxReconnectionAttempts") != null) options.maxReconnectionAttempts = + call.argument("maxReconnectionAttempts")!! + if (call.argument("maxReconnectGapInSeconds") != null) options.maxReconnectGapInSeconds = + call.argument("maxReconnectGapInSeconds")!! + if (call.argument("authEndpoint") != null) options.channelAuthorizer = + HttpChannelAuthorizer(call.argument("authEndpoint")) + if (call.argument("authorizer") != null) options.channelAuthorizer = this + if (call.argument("proxy") != null) { + val (host, port) = call.argument("proxy")!!.split(':') + options.proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port.toInt())) } + pusher = Pusher(call.argument("apiKey"), options) Log.i(TAG, "Start $pusher") result.success(null) } catch (e: Exception) { diff --git a/ios/Classes/SwiftPusherChannelsFlutterPlugin.swift b/ios/Classes/SwiftPusherChannelsFlutterPlugin.swift index f89821e..7d8b4a6 100644 --- a/ios/Classes/SwiftPusherChannelsFlutterPlugin.swift +++ b/ios/Classes/SwiftPusherChannelsFlutterPlugin.swift @@ -35,66 +35,67 @@ public class SwiftPusherChannelsFlutterPlugin: NSObject, FlutterPlugin, PusherDe } func initChannels(call: FlutterMethodCall, result: @escaping FlutterResult) { - if pusher == nil { - let args = call.arguments as! [String: Any] - var authMethod: AuthMethod = .noMethod - if args["authEndpoint"] is String { - authMethod = .endpoint(authEndpoint: args["authEndpoint"] as! String) - } else if args["authorizer"] is Bool { - authMethod = .authorizer(authorizer: self) - } - var host: PusherHost = .defaultHost - if args["host"] is String { - host = .host(args["host"] as! String) - } else if args["cluster"] != nil { - host = .cluster(args["cluster"] as! String) - } - var useTLS: Bool = true - if args["useTLS"] is Bool { - useTLS = args["useTLS"] as! Bool - } - var port: Int - if useTLS { - port = 443 - if args["wssPort"] is Int { - port = args["wssPort"] as! Int - } - } else { - port = 80 - if args["wsPort"] is Int { - port = args["wsPort"] as! Int - } - } - var activityTimeout: TimeInterval? - if args["activityTimeout"] is TimeInterval { - activityTimeout = args["activityTimeout"] as! Double / 1000.0 - } - var path: String? - if args["path"] is String { - path = (args["path"] as! String) - } - let options = PusherClientOptions( - authMethod: authMethod, - host: host, - port: port, - path: path, - useTLS: useTLS, - activityTimeout: activityTimeout - ) - pusher = Pusher(key: args["apiKey"] as! String, options: options) - if args["maxReconnectionAttempts"] is Int { - pusher.connection.reconnectAttemptsMax = (args["maxReconnectionAttempts"] as! Int) - } - if args["maxReconnectGapInSeconds"] is TimeInterval { - pusher.connection.maxReconnectGapInSeconds = (args["maxReconnectGapInSeconds"] as! TimeInterval) + if pusher != nil { + pusher.disconnect() + } + let args = call.arguments as! [String: Any] + var authMethod: AuthMethod = .noMethod + if args["authEndpoint"] is String { + authMethod = .endpoint(authEndpoint: args["authEndpoint"] as! String) + } else if args["authorizer"] is Bool { + authMethod = .authorizer(authorizer: self) + } + var host: PusherHost = .defaultHost + if args["host"] is String { + host = .host(args["host"] as! String) + } else if args["cluster"] != nil { + host = .cluster(args["cluster"] as! String) + } + var useTLS: Bool = true + if args["useTLS"] is Bool { + useTLS = args["useTLS"] as! Bool + } + var port: Int + if useTLS { + port = 443 + if args["wssPort"] is Int { + port = args["wssPort"] as! Int } - if args["pongTimeout"] is Int { - pusher.connection.pongResponseTimeoutInterval = args["pongTimeout"] as! TimeInterval / 1000.0 + } else { + port = 80 + if args["wsPort"] is Int { + port = args["wsPort"] as! Int } - pusher.connection.delegate = self - pusher.bind(eventCallback: onEvent) - result(nil) } + var activityTimeout: TimeInterval? + if args["activityTimeout"] is TimeInterval { + activityTimeout = args["activityTimeout"] as! Double / 1000.0 + } + var path: String? + if args["path"] is String { + path = (args["path"] as! String) + } + let options = PusherClientOptions( + authMethod: authMethod, + host: host, + port: port, + path: path, + useTLS: useTLS, + activityTimeout: activityTimeout + ) + pusher = Pusher(key: args["apiKey"] as! String, options: options) + if args["maxReconnectionAttempts"] is Int { + pusher.connection.reconnectAttemptsMax = (args["maxReconnectionAttempts"] as! Int) + } + if args["maxReconnectGapInSeconds"] is TimeInterval { + pusher.connection.maxReconnectGapInSeconds = (args["maxReconnectGapInSeconds"] as! TimeInterval) + } + if args["pongTimeout"] is Int { + pusher.connection.pongResponseTimeoutInterval = args["pongTimeout"] as! TimeInterval / 1000.0 + } + pusher.connection.delegate = self + pusher.bind(eventCallback: onEvent) + result(nil) } public func fetchAuthValue(socketID: String, channelName: String, completionHandler: @escaping (PusherAuth?) -> Void) { diff --git a/lib/pusher_channels_flutter_web.dart b/lib/pusher_channels_flutter_web.dart index ca17f97..6ddd286 100644 --- a/lib/pusher_channels_flutter_web.dart +++ b/lib/pusher_channels_flutter_web.dart @@ -220,6 +220,10 @@ class PusherChannelsFlutterWeb { } void init(MethodCall call) { + if (pusher != null) { + pusher!.unbind_all(); + pusher!.disconnect(); + } var options = Options(); if (call.arguments['cluster'] != null) { options.cluster = call.arguments['cluster']; diff --git a/pubspec.lock b/pubspec.lock index 1816993..918da74 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -78,7 +78,7 @@ packages: name: lints url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.0.1" matcher: dependency: transitive description: @@ -162,5 +162,5 @@ packages: source: hosted version: "2.1.2" sdks: - dart: ">=2.17.0-206.0.dev <3.0.0" + dart: ">=2.17.0 <3.0.0" flutter: ">=1.20.0" diff --git a/pubspec.yaml b/pubspec.yaml index 1a64977..90513e2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: pusher_channels_flutter description: Pusher Channels Flutter Plugin -version: 2.0.2 +version: 2.1.0 homepage: https://github.com/pusher/pusher-channels-flutter repository: https://github.com/pusher/pusher-channels-flutter issue_tracker: https://github.com/pusher/pusher-channels-flutter/issues