Skip to content

Commit

Permalink
Allow reinit (#80)
Browse files Browse the repository at this point in the history
* allow reinit of pusher

* allow reinit of pusher-js

* Bump to version 2.1.0

Co-authored-by: Niek <[email protected]>
Co-authored-by: Pusher CI <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2022
1 parent 4cff17d commit 0a0cbcf
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 83 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,29 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
result: Result
) {
try {
if (pusher == null) {
val options = PusherOptions()
if (call.argument<String>("cluster") != null) options.setCluster(call.argument("cluster"))
if (call.argument<Boolean>("useTLS") != null) options.isUseTLS =
call.argument("useTLS")!!
if (call.argument<Long>("activityTimeout") != null) options.activityTimeout =
call.argument("activityTimeout")!!
if (call.argument<Long>("pongTimeout") != null) options.pongTimeout =
call.argument("pongTimeout")!!
if (call.argument<Int>("maxReconnectionAttempts") != null) options.maxReconnectionAttempts =
call.argument("maxReconnectionAttempts")!!
if (call.argument<Int>("maxReconnectGapInSeconds") != null) options.maxReconnectGapInSeconds =
call.argument("maxReconnectGapInSeconds")!!
if (call.argument<String>("authEndpoint") != null) options.channelAuthorizer =
HttpChannelAuthorizer(call.argument("authEndpoint"))
if (call.argument<String>("authorizer") != null) options.channelAuthorizer = this
if (call.argument<String>("proxy") != null) {
val (host, port) = call.argument<String>("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<String>("cluster") != null) options.setCluster(call.argument("cluster"))
if (call.argument<Boolean>("useTLS") != null) options.isUseTLS =
call.argument("useTLS")!!
if (call.argument<Long>("activityTimeout") != null) options.activityTimeout =
call.argument("activityTimeout")!!
if (call.argument<Long>("pongTimeout") != null) options.pongTimeout =
call.argument("pongTimeout")!!
if (call.argument<Int>("maxReconnectionAttempts") != null) options.maxReconnectionAttempts =
call.argument("maxReconnectionAttempts")!!
if (call.argument<Int>("maxReconnectGapInSeconds") != null) options.maxReconnectGapInSeconds =
call.argument("maxReconnectGapInSeconds")!!
if (call.argument<String>("authEndpoint") != null) options.channelAuthorizer =
HttpChannelAuthorizer(call.argument("authEndpoint"))
if (call.argument<String>("authorizer") != null) options.channelAuthorizer = this
if (call.argument<String>("proxy") != null) {
val (host, port) = call.argument<String>("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) {
Expand Down
115 changes: 58 additions & 57 deletions ios/Classes/SwiftPusherChannelsFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/pusher_channels_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0a0cbcf

Please sign in to comment.