Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reconnecting issues #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const val PRESENCE_PREFIX = "presence-"

class PusherService : MChannel {
private var _pusherInstance: Pusher? = null
private var _connectionListener: ConnectionListener? = ConnectionListener()

companion object {
var enableLogging: Boolean = false
Expand Down Expand Up @@ -120,7 +121,7 @@ class PusherService : MChannel {
}

private fun connect(result: Result) {
_pusherInstance?.connect(ConnectionListener(), ConnectionState.ALL)
_pusherInstance?.connect(_connectionListener, ConnectionState.ALL)
result.success(null)
}

Expand Down Expand Up @@ -174,7 +175,7 @@ class PusherService : MChannel {
private fun unsubscribe(call: MethodCall, result: Result) {
try {
val src = call.arguments as Map<String, Any>
val args = JSONObject(src);
val args = JSONObject(src)
val channelName = args.getString("channelName")

_pusherInstance?.unsubscribe(channelName)
Expand Down
2 changes: 2 additions & 0 deletions lib/src/contracts/stream_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ abstract class StreamHandler {
/// Add a listener to the event channel stream for pusher,
/// any class that extends [StreamHandler] should use this method.
void registerListener(String classId, dynamic Function(dynamic) method) {
_eventStreamSubscription?.cancel();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this line are you able to receive connection state change events after binding to an event? Because whenever you bind it calls registerListener so wouldn't this line cancel the stream causing all classes using the event channel to not receive any events?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, connection state changes still work after binding because Channel and PusherClient extend this abstract class on their own. bind() and connect() only re-subscribes their own instance.


StreamHandler._listeners[classId] = method;

_eventStreamSubscription =
Expand Down
3 changes: 2 additions & 1 deletion lib/src/pusher/pusher_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class PusherClient extends StreamHandler {
}

Future _init(String appKey, PusherOptions options, InitArgs initArgs) async {
registerListener(classId, _eventHandler);
await _channel.invokeMethod(
'init',
jsonEncode({
Expand Down Expand Up @@ -89,6 +88,8 @@ class PusherClient extends StreamHandler {
/// Initiates a connection attempt using the client's
/// existing connection details
Future connect() async {
registerListener(classId, _eventHandler);

await _channel.invokeMethod('connect');
}

Expand Down